使用Mysqldataadapter.update()插入值

本文关键字:插入 Mysqldataadapter update 使用 | 更新日期: 2023-09-27 18:00:04

string InsertQuery=";MySqlTransaction事务;MySqlConnection con1=新建MySqlConnection();MySqlDataAdapter ODA;字符串服务器=loggerConnectionString.servername;con1.ConnectionString="server="localhost";uid=root;pwd=;database=globasys;";con1.Open();transaction=con1.BeginTransaction();ODA=新MySqlDataAdapter();InsertQuery=";插入tr_config_unit_params"+环境NewLine;InsertQuery+="(unit_param_id、unit_id、unit_param_desc、unit_param_opc_progid、unit_paam_host、unit_pa拉姆_link、unit_param _data_type、unit_pa拉姆_type)"+Environment.NewLine;InsertQuery+="VALUES(@unit_param_id、@unit_id、@munit_param_desc、@unit_paam_opc_progid、@unit_param_host、@unit.param_link、@unit/param_data_type、@unit_param_type)";;ODA。InsertCommand=新的MySqlCommand(InsertQuery,con1);ODA。InsertCommand。参数。添加("@unit_param_id",MySqlDbType.Int32);ODA。InsertCommand。参数。添加("@unit_id",MySqlDbType.Int32);ODA。InsertCommand。参数。添加("@unit_param_desc",MySqlDbType.VarChar);ODA。InsertCommand。参数。添加("@unit_param_opc_progid",MySqlDbType.VarChar);ODA。InsertCommand。参数。添加("@unit_param_host",MySqlDbType.VarChar);ODA。InsertCommand。参数。添加("@unit_param_link",MySqlDbType.VarChar);ODA。InsertCommand。参数。添加("@unit_param_data_type",MySqlDbType.Int32);ODA。InsertCommand。参数。添加("@unit_param_type",MySqlDbType.Int32);ODA。InsertCommand。交易=交易;DataSet ds=新的DataSet();ds=dt;int y=ODA。更新(dt,"tr_config_unit_params");交易Commit();con1.Close();

我已经使用Mysqldataadapter.update()插入了150000行,但查询执行成功,但在MYSQL 中没有行插入数据库表

提前感谢。。。。。

使用Mysqldataadapter.update()插入值

MySqlDataAdapter的Update函数重载。为指定数据集中插入、更新或删除的每一行调用相应的INSERT、UPDATE或DELETE语句。

因此,您可以使用以下代码:

string conn = "server="localhost";uid=root;pwd=;database=globasys;";
using (MySqlConnection con1 = new MySqlConnection(conn))
{
con1.Open();
MySqlTransaction transaction = con1.BeginTransaction();
string InsertQuery = "Insert into tr_config_unit_params " + Environment.NewLine;
InsertQuery += "(unit_param_id,unit_id, unit_param_desc, unit_param_opc_progid, unit_param_host, unit_param_link, unit_param_data_type, unit_param_type) " + Environment.NewLine;
InsertQuery += " VALUES(@unit_param_id,@unit_id,@unit_param_desc, @unit_param_opc_progid, @unit_param_host, @unit_param_link, @unit_param_data_type, @unit_param_type)";;
MySqlCommand command = new MySqlCommand(InsertQuery, con1);
command.Transaction = transaction;
try
{
    //dt is a DataTable
    foreach(DataRow dataRow in dt)
    {
        command.Parameters.Clear()
        command.Parameters.AddWithValue("@unit_param_id", dataRow["unit_param_id"]);
        command.Parameters.AddWithValue("@unit_id", dataRow["unit_id"]);
        command.Parameters.AddWithValue("@unit_param_desc", dataRow["unit_param_desc"]);
        command.Parameters.AddWithValue("@unit_param_opc_progid", dataRow["unit_param_opc_progid"]);
        command.Parameters.AddWithValue("@unit_param_host", dataRow["unit_param_host"]);
        command.Parameters.AddWithValue("@unit_param_link", dataRow["unit_param_link"]);
        command.Parameters.AddWithValue("@unit_param_data_type", dataRow["unit_param_data_type"]);
        command.Parameters.AddWithValue("@unit_param_type", dataRow["unit_param_type"]);
        command.ExecuteNonQuery();
    }
    transaction.Commit();
}
catch(MySqlException mySqlEx)
{
    transaction.Rollback();
    throw mySqlEx;
}
}