c#Dapper 批量插入Mysql

 <connectionStrings>
<add name="sqlconnectionString" connectionString="server=127.0.0.1;database=MyDataBase;User=sa;password=123456;Connect Timeout=1000000"/>
<add name="mysqlconnectionString" connectionString="Database=zyzs_terminal_info;Data Source=192.168.1.220;User Id=root;Password=root;CharSet=utf8;port=3306"/>
</connectionStrings>

插入时格式与sql 不一样格式

没有方括号[]

 string mysqlconnectionString = ConfigurationManager.ConnectionStrings["mysqlconnectionString"].ToString();

        #region 向数据库中添加多条记录 +int Insert(Model.Contacts model)

        public int InsertBaseList(List<BaseTable> model)
{
#region SQL语句
const string sql = @"
INSERT INTO zyzs_terminal_base (license_number,shop_name,user_name,phone_number,shop_address)
VALUES (
@license_number
,@shop_name
,@user_name
,@phone_number
,@shop_address
);select @@IDENTITY"; using (var connection = new MySqlConnection(mysqlconnectionString))
{
connection.Open();
return connection.Execute(sql, model);
}
#endregion
} /// <summary>
/// 向数据库中添加一条记录
/// </summary>
/// <param name = "model" > 要添加的实体 </ param >
/// < returns > 插入数据的ID </ returns >
public int InsertBase(BaseTable model)
{
#region SQL语句
const string sql = @"
INSERT INTO zyzs_terminal_base (license_number,shop_name,user_name,phone_number,shop_address)
VALUES (
@license_number
,@shop_name
,@user_name
,@phone_number
,@shop_address
);select @@IDENTITY";
#endregion
using (var connection = new MySqlConnection(mysqlconnectionString))
{
connection.Open();
return connection.Execute(sql, model);
}
}
#endregion

MySql.Data.MySqlClient   添加引用

这个方法就是简单insert的方式插入,速度很慢.数据量大,应使用Mysql MySqlBulkLoader 批量上传

上一篇:在Mac上使用Visual Studio Code开发/调试.NET Core代码


下一篇:【原创分享·微信支付】C# MVC 微信支付教程系列之现金红包