[英]How to call a stored proc using a generic DB connection in C#?
我有時使用ODBC連接調用C#(。Net 2.0)中的存儲過程,有時使用SQLClient。 我未來我們也必須與Oracle溝通。
我的存儲過程具有輸入/輸出參數和返回值。
CREATE PROCEDURE myProc (@MyInputArg varchar(10), @MyOutputArg varchar(20) output) AS (...) return @@ERROR
我的問題是我無法找到一種存儲命令的方法,無論客戶端是什么,它都可以是通用的。 我正在使用IDbCommand對象。
使用ODBC連接我必須定義:
objCmd.CommandText = "? = CALL myProc (?,?)";
在SQLclient上下文中:
objCmd.CommandText = "myProc";
我真的不想解析我的命令,我確信有一個更好的方法來制作一個通用的!
為了幫助人們重現,您可以在下面找到我如何建立通用數據庫連接。 在我的上下文中,提供程序對象是從配置文件定義的。
// My DB connection string, init is done from a configuration file
string myConnectionStr = "";
// Provider which defined the connection type, init from config file
//object objProvider = new OdbcConnection(); //ODBC
object objProvider = new SqlConnection(); // SQLClient
// My query -- must be adapted switch ODBC or SQLClient -- that's my problem!
//string myQuery = "? = CALL myProc (?,?)"; // ODBC
string myQuery = "myProc"; // SQLClient
// Prepare the connection
using (IDbConnection conn = (IDbConnection)Activator.CreateInstance(typeof(objProvider), myConnectionStr ))
{
// Command creation
IDbCommand objCmd = (IDbCommand)Activator.CreateInstance(typeof(objProvider));
objCmd.Connection = conn;
// Define the command
objCmd.CommandType = CommandType.StoredProcedure;
objCmd.CommandTimeout = 30;
objCmd.CommandText = myQuery;
IDbDataParameter param;
// Return value
param = (IDbDataParameter)Activator.CreateInstance(typeof(objProvider));
param.ParameterName = "RETURN_VALUE";
param.DbType = DbType.Int32;
param.Direction = ParameterDirection.ReturnValue;
objCmd.Parameters.Add(param);
// Param 1 (input)
param = (IDbDataParameter)Activator.CreateInstance(typeof(objProvider));
param.ParameterName = "@MyInputArg";
param.DbType = DbType.AnsiString;
param.Size = 10;
param.Direction = ParameterDirection.Input;
param.Value = "myInputValue";
objCmd.Parameters.Add(param);
// Param 2 (output)
param = (IDbDataParameter)Activator.CreateInstance(typeof(objProvider));
param.ParameterName = "@MyOutputArg";
param.DbType = DbType.AnsiString;
param.Size = 20;
param.Direction = ParameterDirection.Output;
objCmd.Parameters.Add(param);
// Open and execute the command
objCmd.Connection.Open();
objCmd.ExecuteReader(CommandBehavior.SingleResult);
(...) // Treatment
}
謝謝你的時間。
此致,Thibaut。
你可以創建一個圍繞IDbCommand和實現的包裝器(但只有你想:))。
//Note the IDisposable interface
public class MultiSqlCommand : IDisposable
{
//DbConnectionType is a custom enum
public MultiSqlCommand(DbConnectionType connType, DbConnection conn)
{
//initialize members
...
switch(connType)
{
case ADO:
_cmd = new SqlCommand(_connection);
break;
case ODBC:
...
}
}
//As param name you pass the undecorated parameter name (no @, ?, etc.)
public void AddParameter(string strippedName, object value)
{
//this should be an internal function which gives you an SqlParameter formatted for your specific DbConnectionType
object parameter = GetSqlParam(strippedName, value);
_cmd.Parameters.Add(object);
}
}
添加參數后,您可以准備執行命令。 您可以通過屬性公開它並在此類之外執行它,或者擴展API並添加一些方法來執行它並獲得結果。 無論你覺得什么方便。
注意:在IDisposable實現中,您應該處置IDbCommand實例(如果不為null)。 這非常重要,否則你會泄漏內存。
如果建議不明確,那么請告訴我,我會嘗試填寫更多細節。 我想說,解決方案很簡單。 您可以更進一步,在整個ADO.NET命令和參數支持的基礎上創建一個抽象層,但我認為這不是必要的。 這不像你沒有寫一個完整的數據提供者。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.