C#:如何在MySQL的insert语句中动态插入多个参数

C#:如何在MySQL的insert语句中动态插入多个参数,c#,asp.net,mysql,C#,Asp.net,Mysql,在我的场景中,我想插入包含15列数据的sql语句。是否有其他方法来处理insert语句的参数 public bool GetCustomersList(ref DataSet dataset, String x, String y, string z, string x1, string y1, string z1 ) { MySqlConnection SQLConn = GetConnection(); try {

在我的场景中,我想插入包含15列数据的sql语句。是否有其他方法来处理insert语句的参数

    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中处理这种情况?

我建议您使用:

command.Parameters.Add( ... );
这样就可以了

            List<SqlParameter> myParamList = new List<SqlParameter>();
            SqlParameter myParam = default(SqlParameter);


            myParam = new SqlParameter("@RoomID", SqlDbType.Int);
            myParam.Value = x
            myParamList.Add(myParam);
List myParamList=new List();
SqlParameter myParam=默认值(SqlParameter);
myParam=新的SqlParameter(“@RoomID”,SqlDbType.Int);
myParam.Value=x
myParamList.Add(myParam);
基本上,您可以创建参数列表,并使用新的SqlParameter为每个参数添加它,注意项目名称和项目类型

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

来自:

使用CreateParameter方法创建具有 指定的名称、类型、方向、大小和值。您传递的任何值 在中,参数被写入相应的参数 财产

使用内联参数是不安全的,可能会导致sql注入。改为使用
SqlParameter
是为.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;
}
同样,您会抛出冗余的特定类类型,如:
MySqlCommand
SqlParameter
。此外,没有必要使用
try{}catch{}
<代码>使用语句将关闭dispose上的连接,即使抛出异常也是如此。如果不需要,请不要在每次请求时/之后打开和关闭连接。利用