Search code examples
c#stored-proceduressap-ase

Generic Sybase stored procedure execution in C#


I'd like to execute Sybase stored procedure the same way I am doing in a SQL IDE, i.e. something like this in SQL:

exec sp_GetCMyDataPerRegion JAPAN'

However, instead of this, in the C# code I am forced to define each parameter individually, each having all those types defined:

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);

Quite a pain and not finding anyway so far to have it "generic", i.e. would just like to wrap the store procedure call in a method with this kind of signature:

public AseDataReader ExecuteStoredProcedure(string spExecutionString){}

Would you have any way on doing so?

Thank you in advance!


Solution

  • I do have an example of an SqlDataReader where the Function call is

    ExecuteNonQuery("dbo.[Sp_Skp_UpdateFuncties]", parameters);
    

    This is in a class DataBaseManager which hold the databaseconnectionstring

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