总体而言update 更新上传速度还是慢.
1: 简单的insert 速度稍稍比MySqlDataAdapter慢一点
配合dapper
配置文件
<?xml version="1.0" encoding="utf-8"?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/> </startup> <connectionStrings> <add name="sqlconnectionString" connectionString="server=127.0.0.1;database=MyDataBase;User=sa;password=123456;Connect Timeout=1000000"/> <!--<add name="mysqlconnectionString" connectionString="Database=terminal_info;Data Source=127.0.0.1;User Id=root;Password=123456;CharSet=utf8;port=3306"/>--> <add name="mysqlconnectionString" connectionString="Database=terminal_info;Data Source=192.168.1.220;User Id=root;Password=root;CharSet=utf8;port=3306"/> </connectionStrings> </configuration>
string connectionString = ConfigurationManager.ConnectionStrings["mysqlconnectionString"].ToString();
public int Update(BaseTable model) { #region SQL语句 const string sql = @" UPDATE terminal_base SET license_number2 = @license_number2 WHERE license_number = @license_number"; #endregion using (MySqlConnection connection = new MySqlConnection(connectionString)) { return connection.Execute(sql, model); } }
public class BaseTable { public string license_number { get;set; } //客户编码 public string shop_name { get; set; } //店铺名称 public string user_name { get; set; } //姓名 public string phone_number { get; set; } //手机号 }
更新5万数据用了十来分钟吧.单线程处理的
2用MySqlDataAdapter上传 这个坑不少,网上资料也很少, fill是 从当前数据库查询到数据填充到dataset, update是把 改完的dataset 上传到mysql .
/// <summary> ///使用MySqlDataAdapter批量更新数据 /// </summary> /// <param name="connectionString">数据库连接字符串</param> /// <param name="table">数据表</param> public void BatchUpdate(List<BaseTable> model) { DataTable table = ListToDatatable.ToDataTable(model); table.TableName = "terminal_base"; table.ExtendedProperties.Add("SQL", "select license_number,license_number2 from terminal_base"); MySqlConnection connection = new MySqlConnection(connectionString); connection.Open(); //创建数据适配器 string sqlcommd= "select license_number,license_number2 from zyzs_terminal_base"; MySqlDataAdapter adapter = new MySqlDataAdapter(sqlcommd, connection); DataSet ds = new DataSet();//创建数据集 adapter.Fill(ds, "terminal_base"); // ds.Tables.Clear(); //给SqlDataAdapter的UpdateCommand属性指定执行更新操作的SQL语句select * from zyzs_terminal_base adapter.UpdateCommand = new MySqlCommand("update terminal_base set license_number2 = @license_number2 where [email protected]_number", connection); //添加参数并赋值 adapter.UpdateCommand.Parameters.Add("@license_number2", MySqlDbType.VarChar, 50, "license_number2"); MySqlParameter prams_ID = adapter.UpdateCommand.Parameters.Add("@license_number", MySqlDbType.VarChar); prams_ID.SourceColumn = "license_number"; prams_ID.SourceVersion = DataRowVersion.Original; //填充数据集 //调用Update方法提交更新后的数据集ds,并同步更新数据库数据 adapter.Update(ds, "terminal_base"); // transaction.Commit(); //MySqlCommand command = connection.CreateCommand(); //command.CommandTimeout = 4000; //command.CommandType = CommandType.Text; //MySqlDataAdapter adapter = new MySqlDataAdapter(command); //MySqlCommandBuilder commandBulider = new MySqlCommandBuilder(adapter); //commandBulider.ConflictOption = ConflictOption.OverwriteChanges; //MySqlTransaction transaction = null; //try //{ // connection.Open(); // transaction = connection.BeginTransaction(); // //设置批量更新的每次处理条数 // adapter.UpdateBatchSize = 1000; // //设置事物 // adapter.SelectCommand.Transaction = transaction; // if (table.ExtendedProperties["SQL"] != null) // { // adapter.SelectCommand.CommandText = table.ExtendedProperties["SQL"].ToString(); // } // adapter.Fill(ds, "zyzs_terminal_base"); // adapter.Update(table); // transaction.Commit();/////提交事务 // } //catch (MySqlException ex) //{ // if (transaction != null) transaction.Rollback(); // throw ex; //} //finally //{ // connection.Close(); // connection.Dispose(); //} } //public int Update(Model.Model model) //{ // #region SQL语句 // const string sql = @" // UPDATE [dbo].[Contacts] // SET // [UserName] = @UserName // ,[Tel] = @Tel // ,[Tel1] = @Tel1 // ,[Address] = @Address // WHERE [Id] = @Id"; // #endregion // using (SqlConnection connection = new SqlConnection(connstr)) // { // return connection.Execute(sql, model); // } //} #endregion
原文地址:https://www.cnblogs.com/zuochanzi/p/9140854.html
时间: 2024-11-10 20:09:18