0

我在从 Sybase API 中的 AseCommand 对象运行 Sybase SQL 脚本时遇到问题。这是我的脚本的副本,它作为 sql 字符串参数传递到 C#

SQL 脚本:

DECLARE @dt DATETIME
DECLARE @mx INT

SELECT @dt = getDate()


EXEC DATABASE_NAME..StoredProcedure1
    @id_own_grd  =  200,
    @id_dom_grd  =  'TEST VALUE',
    @value       =  @mx OUTPUT

EXEC DATABASE_NAME..StoredProcedure2
    @id_level = @mx, @id_level_pt=NULL, @id_obj1 = 58464819, 
    @id_typ_lvl='TEST', @am_test=20130916,  @id_obj1_sub = 12949, 
    @dt_start_lvl = '9/16/2013', @dt_end_lvl = '9/16/2013',@ct_blah=0,
    @id_abs=" ", @id_obj1_trans=0,@am_obj1_vol=NULL, @am_obj3=-0.311095,
    @id_obj_test = NULL, @id_obj5_test = NULL, @id_is_valid = '0',
    @nm_db_origin='ORIGIN_DB', @id_owner=200

调用上述 SQL 脚本的 C# 代码:

public Dictionary<int, Dictionary<string, object>> ExecuteSqlQuery(string sql)
{
    lock (this)
    {
        try
        {
            using (AseCommand command = new AseCommand(sql, Connection))
            using (AseDataReader reader = command.ExecuteReader())
            {
                Log.Info("Executing Query: [" + sql + "] on " + DbSettings);
                command.CommandTimeout = 120;
                var results = new Dictionary<int, Dictionary<string, object>>();
                var columns = new List<string>();
                for (int i = 0; i < reader.FieldCount; i++)
                {
                    columns.Add(reader.GetName(i));
                }
                int row = 0;
                while (reader.Read())
                {
                    Dictionary<string, object> rowResults = new Dictionary<string, object>();
                    results.Add(row, rowResults);
                    foreach (string item in columns)
                    {
                        rowResults.Add(item, reader[item]);
                    }
                    row++;
                }
                Log.Info("Finished Executing Query: [" + sql + "] on " + DbSettings + " in " + timer.TimeTaken());
                return results;
            }
        }
        catch (Exception e)
        {
            Log.Error("An error occured executing query SQL: " + e.Message, e);
            throw;
        }
    }
}

每当我尝试运行上述脚本时,都会抛出并捕获一个 Sybase.Data.AseClient.AseException,上面写着“参数数量无效”。此 C# 代码适用于不调用存储过程的任何其他代码。我知道 Sybase 有调用存储过程的特定步骤,尤其是输出参数,详见此链接:

http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.infocenter.dc20066.0215/html/adonet2155/Procedures_adodotnet_development.htm

但是,我的用户希望能够在一个脚本中调用多个存储过程——我还不知道该怎么做。有人有什么想法吗?

谢谢,

4

1 回答 1

1

我和一位同事找到了答案。您必须更改 Sybase 连接字符串中的标志。将此添加到连接字符串

;Named Parameters=false

这告诉 Ase 客户端不要检查传递给存储过程的每个参数。如果它爆炸了,它完全在调用者身上。您还需要在使用 OUTPUT 参数的任何时候设置 NamedParameters = false。

于 2013-09-19T12:07:19.723 回答