在ADO環境下,調用預存程序查詢資料時常規做法為:
1 建立Connection Command對象
2 開啟串連,給Command賦參數的名稱、資料類型、值
3 執行Command對象
4 返回給Recordset對象交給用戶端
這樣做每調用一次預存程序都要按照預存程序中的參數的資料類型建立Parameters對象
比如預存程序需要兩個參數 @ID int、@Name varchar(10)就需要
‘建立參數
cmd.Parameters.Append cmd.CreateParameter("@ID",adInteger,adParamInput,4)
cmd.Parameters.Append cmd.CreateParameter("@Name",adVarChar,adParamInput,10)
‘給參數賦值
cmd("@State") = 1
cmd("@WhereT")=”2”
每調用一個預存程序都要手工添加這個預存程序的所有參數,用自己的腦力保證參數的資料類型和預存程序中的參數的資訊的一致性。
Command.Parameters對象有一個Refresh方法,這個方法的作用時讀取當前Command對象需要的所有參數的名稱和資料類型,用這個方法就可以寫成一個調用所有預存程序的共用函數,現面這個函數完成了一個返回結果集的預存程序的通用函數。很簡單可以根據需要細化。
‘在VisualBasic6.0調試通過。
Function GetRsByPro(strConnString As String, strProName As String, arjParameter() As String)
' 返回 查詢的記錄集
' strConnString 資料連線串
' strProName 預存程序名
' arjParameter() 預存程序需要的數組
On Error GoTo errMsg
'建立ADO對象
Dim Cmd As New Command
' ASP Con = Server.CreateObject("ADODB.Connection")
Dim Con As New Connection
' ASP Set Cmd = Server.CreateObject("ADODB.Command")
Dim Rs As New Recordset
' ASP Set rs = Server.CreateObject("ADODB.Recordset")
'開啟資料庫
Con.Open strConnString
Set Cmd.ActiveConnection = Con
Cmd.Commandtype = adCmdStoredProc
Cmd.Parameters.Refresh
If UBound(arjParameter) <> Cmd.Parameters.Count Then
Debug.Print "參數個數不對"
Exit Function
End If
'給預存程序參數賦值
For i = 0 To Cmd.Parameters.Count - 1
Cmd.Parameters(i).Value = arjParameter(i)
Next
'設定Recordset對象
Rs.CursorType = 3
Rs.LockType = 3
Rs.CursorLocation = 3
Set Rs.Source = Cmd
Rs.Open
'返回結果集
Set GetRsByPro = Rs
'關閉資料來源
Con.Close
Set Con = Nothing
errMsg:
Debug.Print Err.Description
End Function
‘調用Demo
Dim Rs As New Recordset
StrConnString=””
StrProName=”pro_GetAllUser”
Dim arjParameter(1)
arjParameter(0)=”1”
arjParameter(1)=”山東”
Set Rs= GetRsByPro(strConnString, strProName, arjParameter())
用相同的方法在.NET開發環境裡也可以建立一個通用的方法調用預存程序。
在ADO.NET裡不管是OleDbCommand.Parameters對象還是SqlCommand.Parameters對象都沒有Refresh方法讀取預存程序的參數資訊,.NET在OleDbCommandBuilder類裡提供了一個DeriveParameters靜態方法可以實現相同的功能。
.NET SDK裡關於DeriveParameters的描述
“使用在 SqlCommand 中指定的預存程序的參數資訊,填充指定的 SqlCommand 對象的 Parameters 集合。”
SqlConnection Conn=new SqlConnection(cnString);
Conn.Open();
SqlCommand Comm=new SqlCommand();
Comm.Connection =conn;
Comm.CommandType =CommandType.StoredProcedure ;
Comm.CommandText =proName;
SqlCommandBuilder.DeriveParameters(comm);
//經過這個方法後SqlCommand對象的SqlParameters對象已經幫定了預存程序中的資訊了
實現執行任意一個預存程序返回一個DataSet對象的具體函數代碼
檔案名稱 :TestSqlAccess.cs
// 在vs.net調試通過
using System;
using System.Data;
using System.Xml;
using System.Data.SqlClient;
using System.Data.OleDb ;
using System.Collections;
namespace Erp
{
public sealed class TestSqlAccess
{
#region 擷取預存程序參數集合
public static SqlParameter [] getParameters(string cnString,string proName)
{
SqlConnection conn=new SqlConnection(cnString);
conn.Open();
SqlCommand comm=new SqlCommand();
comm.Connection =conn;
comm.CommandType =CommandType.StoredProcedure ;
comm.CommandText =proName;
SqlCommandBuilder.DeriveParameters(comm);
SqlParameter [] arPrm=new SqlParameter[comm.Parameters.Count];
for (int i=0;i<comm.Parameters.Count;i )
{
arPrm[i]=new SqlParameter();
arPrm[i].SqlDbType =comm.Parameters[i].SqlDbType ;
arPrm[i].ParameterName=comm.Parameters[i].ParameterName;
arPrm[i].Size =comm.Parameters[i].Size;
}
return arPrm;
}
#endregion
#region 執行Command對象返回DataSet
/////可以調用微軟提供的那個SqlHelper類..
#endregion 執行Command對象返回DataSet