【问题标题】:C#: How to insert multiple param dynamically in Insert Statement in MySQLC#:如何在 MySQL 的 Insert 语句中动态插入多个参数
【发布时间】:2012-11-05 12:27:45
【问题描述】:

在我的场景中,我想插入具有 15 列数据的 sql 语句。有没有其他方法来处理插入语句的参数?

    public bool GetCustomersList(ref DataSet dataset, String x, String y, string z, string x1, string y1, string z1 )
    {
        MySqlConnection SQLConn = GetConnection();
        try
        {
            string get_records;
            if (SQLConn.State == ConnectionState.Closed)
            {
                SQLConn.Open();
            }
            if (searchtype == "x")
            {
                get_records = "INSERT into table values(x, y,z,x1,y1,z1);
            }
            MySqlCommand command = new MySqlCommand(get_records, SQLConn);
            MySqlDataAdapter mySqlAdapter = new MySqlDataAdapter(command);
            mySqlAdapter.Fill(dataset);

            if (SQLConn.State == ConnectionState.Open)
            {
                SQLConn.Close();
            }
            return true;
        }
        catch (Exception )
        {
            if (SQLConn.State == ConnectionState.Open)
            {
                SQLConn.Close();
            }
            return false;
        }
    }

这里的参数可能会扩展到15或更多?在asp.net中如何处理这种情况?

【问题讨论】:

    标签: c# asp.net mysql


    【解决方案1】:

    我建议你使用:

    command.Parameters.Add( ... );
    

    【讨论】:

      【解决方案2】:

      这样就可以了。

                  List<SqlParameter> myParamList = new List<SqlParameter>();
                  SqlParameter myParam = default(SqlParameter);
      
      
                  myParam = new SqlParameter("@RoomID", SqlDbType.Int);
                  myParam.Value = x
                  myParamList.Add(myParam);
      

      基本上,您创建参数列表,并使用新的 SqlParameter 为每个参数添加它,注意项目名称和项目类型。

      然后将参数列表添加到命令中。

      【讨论】:

        【解决方案3】:

        来自MSDN

        使用 CreateParameter 方法创建一个新的 Parameter 对象,其中包含 指定名称、类型、方向、大小和值。您传递的任何值 在参数中写入相应的Parameter 属性。

        使用内联参数是不安全的,可能会导致 sql 注入。改用SqlParameterprefered and more secure 为.Net 中的sql 语句提供参数的方式。

        另外,如果你想更纯粹,同样摆脱依赖于代码中的特定实现,请改用更多接口,例如:

        using (var connection = GetConnection())
        {
            connection.Open();
            using (var command = connection.CreateCommand())
            {
                command.CommandType = CommandType.Text;
                command.CommandText = "INSERT into table(x, y) VALUES (@x, @y)";
                command.Parameters.Add(CreateParameter(command, "@x", x, DbType.String));
                command.Parameters.Add(CreateParameter(command, "@y", y, DbType.String));
                command.ExecuteNonQuery();
            }
        }
        

        您的自定义 CreateParameter 包装器具有以下实现:

        private IDataParameter CreateParameter(IDbCommand command, string name, object value, DbType type)
        {
            var parameter = command.CreateParameter();
            parameter.ParameterName = name;
            parameter.Value = value;
            parameter.DbType = type;
            return parameter;
        }
        

        同样,您丢弃了冗余的特定类类型,例如:MySqlCommandSqlParameter。此外,没有必要使用try{} catch{}Using 语句将在 dispose 时关闭连接,即使抛出异常也是如此。如果您不需要,请不要在每次请求时/之后打开和关闭连接。充分利用connection pooling

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-05-08
          • 1970-01-01
          相关资源
          最近更新 更多