总体而言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上传 这个坑不少,网上资料也很少, 下边的方式是从web版本 serversql的写法改进而来, 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 license_number=@license_number", connection); //添加参数并赋值
adapter.UpdateCommand.Parameters.Add("@license_number2", MySqlDbType.VarChar, , "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 编译器上下文的问题, 搜搜百度,改改调试里边某项设置就可以了.