C#中的通用Sybase存储过程执行

时间:2013-08-14 05:00:17

标签: c# stored-procedures sybase-ase

我想像在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){}

你有没有办法这样做?

提前谢谢!

1 个答案:

答案 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();
}
....
}