【问题标题】:Sqlparameters - pass them to a methodSqlparameters - 将它们传递给方法
【发布时间】:2012-10-31 08:26:04
【问题描述】:

我有一个这样的方法:

private void SetDataSet(string sqlString, params SqlParameter[] parameters)
    {
        DataSet ds = new DataSet();
        using (SqlConnection conn = new SqlConnection(cs))     
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.Connection = conn;
                //cmd.CommandType = CommandType.
                cmd.CommandText = sqlString;

                if (parameters != null)
                {
                    foreach (SqlParameter parm in parameters)
                    {
                        cmd.Parameters.Add(parm);
                    }
                }

                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                     cmd.ExecuteScalar();
                }
            }
        }

    }

现在,从另一个名为 testMethod 的方法中,我想用所有需要的参数调用“SetDataSet”。不幸的是,我不知道如何“构造”所需的 sqlparameters 以及以后如何将信息传递给它们“SetDataSet”方法。

private void testMethod()
{
  string sqlString = .... .

  //here should be the code, which will create sql parameters

  //and now we call the SetDataSet with all needed arguments:
  SetDataSet(sqlString, ?!);
}

我正在考虑某种循环,它可以在 testMethod 中创建 sqlparameters 数组(?),然后将其传递给 SetDataSet 方法,但不知道如何实现。

有什么想法吗?

【问题讨论】:

  • 看来你需要学习一下集合和数组。
  • 你的参数从何而来?
  • 如果你在testMethod生成查询,为什么不把参数一次性嵌入到字符串中呢?
  • 您的方法中不需要SqlDataAdapterDataSet 也是多余的。检查新创建的连接是否关闭也是没有意义的。我建议不要将 sql 字符串和参数传递给方法,而是创建像 InsertProduct(具有适当的强类型属性)或其他任何方法,因为这样更不容易出错且更可重用。

标签: c# sql methods parameters


【解决方案1】:

您需要创建 SQL 参数吗?

像这样:

SqlParameter SqlParm = new SqlParameter("ID", SqlDbType.Int);
SqlParm.Value = 100;

如果您认为您正在使用某种通用方式来处理所有数据库内容,那么您最终会创建一个新层,但无法避免访问数据库的具体细节。

【讨论】:

    【解决方案2】:

    您所要做的就是提供参数。 params 参数会自动创建数组。

     SetDataSet(sqlString, param1, param2, param3, ..., paramN);
    

    【讨论】:

      【解决方案3】:

      您需要创建 SQL 参数吗?

      像这样:

      SqlParameter sqlP1= new SqlParameter("Id", SqlDbType.Int);
      sqlP1.Value = 200;
      

      【讨论】:

        【解决方案4】:

        所以你想要做的是这样的:

        private void testMethod() 
        {
            string sqlString = .....
        
            //here should be the code, which will create sql parameters
        
            // An varchar(80) parameter called @Name with the value "Chuck".
            SqlParameter paramName = new SqlParameter("@Name", SqlDbType.VarChar, 80);
            paramName.Value = "Chuck";
        
            // An int parameter called @Age with the value 49.
            SqlParameter paramAge = new SqlParameter("@Age", SqlDbType.Int);
            paramAge.Value = 49;
        
            // Create more parameters here, as many as you want.
            // You could also create a SqlParameter[] array and send in instead.
        
            //and now we call the SetDataSet with all needed arguments:
            SetDataSet(sqlString, paramName, paramAge); // just add all parameters one after another.
        }
        

        由于您在SetDataSet(string sqlString, params SqlParameter[] parameters) 中使用了params 参数,因此您可以在sqlString 参数之后添加零或您想要的参数数量。

        【讨论】:

          猜你喜欢
          • 2012-12-18
          • 1970-01-01
          • 2017-04-24
          • 1970-01-01
          • 2012-05-09
          • 2015-11-04
          • 2011-01-01
          • 2021-04-25
          • 2014-06-03
          相关资源
          最近更新 更多