我想像在SQL IDE中那样执行Sybase存储过程,就像在SQL中这样:
exec sp_GetCMyDataPerRegion JAPAN'
然而,在C#代码中,我不得不单独定义每个参数,每个参数都定义了所有类型:
AseCommand command = new AseCommand(spName, DbConnection);
command.CommandType = CommandType.StoredProcedure;
AseParameter param = command.CreateParameter();
param.ParameterName = "@region";
param.AseDbType = AseDbType.VarChar;
param.Direction = ParameterDirection.Input;
param.Value = myValue;
command.Parameters.Add(param);
相当痛苦,到目前为止还没有找到它" generic",即只想将存储过程调用包装在带有这种签名的方法中:
public AseDataReader ExecuteStoredProcedure(string spExecutionString){}
你有没有办法这样做?
提前谢谢!
答案 0 :(得分:1)
我有一个SqlDataReader的例子,其中Function调用是
ExecuteNonQuery("dbo.[Sp_Skp_UpdateFuncties]", parameters);
这是一个包含databaseconnectionstring
的DataBaseManager类public classDataBaseManager
{
...
public int ExecuteStoredProcedure(string storedprocedureNaam, IEnumerable<KeyValuePair<string, object>> parameters)
{
var sqlCommand = new SqlCommand
{
Connection = DatabaseConnectie.SqlConnection,
CommandType = CommandType.StoredProcedure,
CommandText = storedprocedureNaam,
};
foreach (KeyValuePair<string, object> keyValuePair in parameters)
{
sqlCommand.Parameters.Add(
new SqlParameter { ParameterName = "@" + keyValuePair.Key, Value = keyValuePair.Value ?? DBNull.Value }
);
}
if (sqlCommand == null)
throw new KoppelingException("Stored procedure ({0}) aanroepen lukt niet", storedprocedureNaam);
return sqlCommand.ExecuteNonQuery();
}
....
}