c#mysql批量更新的两种⽅法
总体⽽⾔update 更新上传速度还是慢.
1:  简单的insert  速度稍稍⽐MySqlDataAdapter慢⼀点
配合dapper
配置⽂件
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>手机mysql安装配置教程
</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();
counterpartspublic int Update(BaseTable model)
{
#region SQL语句
const string sql = @"
UPDATE  terminal_base
SET  license_number2 = @license_number2  WHERE license_number = @license_number";
#endregionnetty实现gossip
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; } //⼿机号
}
78模板网免费模板
更新5万数据⽤了⼗来分钟吧.单线程处理的
2⽤MySqlDataAdapter上传这个坑不少,⽹上资料也很少,  下边的⽅式是从web版本 serversql的写法改进⽽来, fill是从当前数据库查询到数据填充到dataset, update是把改完的dataset 上传到mysql  .
///<summary>
///使⽤MySqlDataAdapter批量更新数据
///</summary>
///<param name="connectionString">数据库连接字符串</param>
///<param name="table">数据表</param>
public void BatchUpdate(List<BaseTable> model)
controller of customs
{
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 license_number=@license_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");
}
#endregion
这个⽅法我是报了错,clr 编译器上下⽂的问题, 搜搜百度,改改调试⾥边某项设置就可以了.

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。