【问题标题】:How to create SqlParameterCollection with multiple parameters?如何创建具有多个参数的 SqlParameterCollection?
【发布时间】:2014-04-27 07:42:41
【问题描述】:

我正在尝试创建一个SqlParameterCollection,但在sp.Add() 方法中添加一些SqlParameter 时出错。

请帮助我如何添加参数以及如何将其传递给我声明SqlConnectionSqlCommand 的另一个函数。

SqlParameterCollection sp = null;                    
sp.Add(new SqlParameter("@CmpyCode", SqlDbType.NVarChar)).Value = CV.Global.CMPYCODE;
sp.Add(new SqlParameter("@Code", SqlDbType.NVarChar)).Value = codeName;
sp.Add(new SqlParameter("@DisplayCode", SqlDbType.NVarChar)).Value = codeName + "-";
sp.Add(new SqlParameter("@TotalDigit", SqlDbType.Int)).Value = CV.Global.PARAMTOTALDIGIT;
insertData("<Sp Name>", sp);

我的另一个函数是 insertData(...)

internal static int insertData(string spName, SqlParameterCollection sp)
{
        int retObj = 0;

        using (SqlConnection con = new SqlConnection(CV.Global.CONSTRING))
        {
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand(spName, con);
                cmd.CommandType = CommandType.StoredProcedure;

                if (sp.Count > 0)
                {
                    foreach (SqlParameter param in sp)
                        cmd.Parameters.Add(param);
                }

                retObj = cmd.ExecuteNonQuery();
            }
            catch (Exception ev) 
            { 
                Util.Log(ev); 
                throw; 
            }
            finally
            {
                try
                {
                    con.Close();
                }
                catch (Exception ev) { Util.Log(ev); throw; }
            }
        }
        return retObj;
    }

我正在尝试创建一个SqlParameterCollection 并将其传递给insertData 函数。但是当我在第一个函数中调用sp.Add() 方法时,它会引发错误。

错误是

对象引用未设置为对象的实例

【问题讨论】:

  • 尝试使用sqlcommand和Parameters.AddWithValue
  • 您永远不会告诉我们您遇到了什么错误,如果您不确切说明发生了什么,您希望我们如何提供帮助。
  • 它给出错误“对象引用未设置为对象的实例”

标签: c# sql


【解决方案1】:

你不能使用像SqlParameterCollection(引用对象)这样的任何变量而不调用它的构造函数(new),但是SqlParameterCollection是一个不能直接用new初始化的对象。它没有公共构造函数,只能从现有SqlCommand 的属性中检索。

 SqlCommand cmd = new SqlCommand(commandText, connection);
 SqlParameterCollection sp = cmd.Parameters;

我建议将您的InsertData 方法更改为接受List&lt;SqlParameter&gt;,并让它处理将参数添加到执行命令文本的SqlCommand

List<SqlParameter> sp = new List<SqlParameter>()
{
    new SqlParameter() {ParameterName = "@CmpyCode", SqlDbType = SqlDbType.NVarChar, Value= CV.Global.CMPYCODE},
    new SqlParameter() {ParameterName = "@Code", SqlDbType = SqlDbType.NVarChar, Value = codeName},
    new SqlParameter() {ParameterName = "@DisplayCode", SqlDbType = SqlDbType.NVarChar, Value = codeName + "-"},
    new SqlParameter() {ParameterName = "@TotalDigit", SqlDbType = SqlDbType.Int, Value = CV.Global.PARAMTOTALDIGIT}
};
insertData(CV.Sps.SP_INSERT_PARAM_TABLE, sp);

insertData 只是接收一个可选的 SqlParameter 列表,并在需要时将它们添加到内部 SqlCommand 参数集合中

internal static int insertData(string spName, List<SqlParameter> sp = null)
{
    ....
    if(sp != null)
        cmd.Parameters.AddRange(sp.ToArray());
    ....
}

【讨论】:

  • 哦,是的,你是对的,只是忙于修复参数列表初始化中的一些问题
  • @Steve 它给出了错误,因为我想同时添加带有SqlDbTypeValue 的SqlParameter,您的代码只允许("@ParameterName") and (object value) ,但我还想提一下SqlDbType
  • 我正在使用kudvenkat's 教程将异常记录到仅适用于一个 SqlParameter 的数据库中。我想整合您的列表解决方案。在我的示例中,我们将 SqlParamter 设置为:SqlParameter parameter = new SqlParameter("@exceptionMessage", log); cmd.Parameters.Add(parameter); 如何更改此 SqlParameter 格式以适合您的 new SqlParameter() {ParameterName = "@CmpyCode", SqlDbType = SqlDbType.NVarChar, Value= CV.Global.CMPYCODE} 格式?如果可能的话,我想排除除@exceptionMessage 之外的所有内容..
  • @KyleVassella 抱歉,我没有参考本教程。我建议您发布一个新问题,其中包含有关您当前解决方案的详细信息,添加您尝试整合此答案的内容,并且您可能会有比我更多的人来查看您的问题
猜你喜欢
  • 1970-01-01
  • 2014-09-13
  • 1970-01-01
  • 2015-10-06
  • 1970-01-01
  • 2018-01-11
  • 2021-11-21
  • 1970-01-01
  • 2021-12-17
相关资源
最近更新 更多