C#??MySQL?????????μ??Ч?????д??
???????????? ???????[ 2016/11/16 17:45:23 ] ??????????????????? MySQL
???????
??????????????????????????????????д???????????????????????????????????????????G????????????? ?????????????C#?????????????MySQL??
????????????????? select ?????????????C#?ж???????д???? ???????????????С? ?????? select -> process -> insert???????衣 ??????????С??????£??????? or ??????)????
??????1??С?????????????????????????????????????? ??????????????????????
??????????????????????????
????????? ????????????
???????????????????к???????о??°??
????1. ????????-??????????????ORM????????EF??NHibernat ?????????
????2. ????????-AK47?? ???Dapper??PetaPoco?????cs?????????Ч????ü????????????????????PetaPoco :)??
????3. ???????????????????????Connection??Command?? ???д?????SQL?????
??????????
??????????????????????????????PASS?? ???????????????????С?
??????????????Dapper??PetaPoco ??????????????????????????IL????漼?????????????????Ч???PASS
?????e????????????????SQL???? ????DataReader ???и?Ч????????????????????????????????????????
??????????????£?
using (var conn = new MySqlConnection("Connection String..."))
{
conn.Open();
//????????????????????????????????????
var c = new MySqlCommand("set net_write_timeout=9999999; set net_read_timeout=9999999"?? conn);
c.ExecuteNonQuery();
MySqlCommand rcmd = new MySqlCommand();
rcmd.Connection = conn;
rcmd.CommandText = @"SELECT `f1`??`f2` FROM `table1`";
//???????????г??
rcmd.CommandTimeout = 99999999;
var myData = rcmd.ExecuteReader();
while (myData.Read())
{
var f1= myData.GetInt32(0);
var f2= myData.GetString(1);
//?????????????....
}
}
???????????????????????????????????????????????????????? ????????????????????
????????? ???????
???????????????????????????????????????? ??????????Щ?????????????????????????????????????C#??????????????? ?????θ?Ч??д?????????????
???????????????д?? ???????CLR via C# ????????????? ??O(∩_∩)O??????~ ????????????
?????????? ???????
?????????????????Ч??? ????????? ?????????BeginTransaction?? ???EndTransaction?? ????????????????????Ч??? ??????и????Ч?????????????insert???
????????????????
????insert into table (f1??f2) values(1??'sss')??values(2??'bbbb')??values(3??'cccc');
???????values??????????????????????????????????? ??
????????????????????100MB??SQL??У?MySQL???????????????????????????????? ??? MySQL?????????max_allowed_packet ?????????? ?????1MB
??????????????α?????
//???StringBuilder??Ч????????
var sqlBuilder = new StringBuilder();
//???insert ?????
string sqlHeader = "insert into table1 (`f1`??`f2`) values";
sqlBuilder.Append(sqlHeader);
using (var conn = new MySqlConnection("Connection String..."))
{
conn.Open();
//????????????????????????????????????
var c = new MySqlCommand("set net_write_timeout=9999999; set net_read_timeout=9999999"?? conn);
c.ExecuteNonQuery();
MySqlCommand rcmd = new MySqlCommand();
rcmd.Connection = conn;
rcmd.CommandText = @"SELECT `f1`??`f2` FROM `table1`";
//???????????г??
rcmd.CommandTimeout = 99999999;
var myData = rcmd.ExecuteReader();
while (myData.Read())
{
var f1 = myData.GetInt32(0);
var f2 = myData.GetString(1);
//?????????????....
sqlBuilder.AppendFormat("({0}??'{1}')??"?? f1??AddSlash(f2));
if (sqlBuilder.Length >= 1024 * 1024 * 1024)//????????1MB length??????????????? 1MB??Packet???????????????
{
insertCmd.Execute(sqlBuilder.Remove(sqlBuilder.Length-1??1).ToString())//??????????????
sqlBuilder.Clear();//???
sqlBuilder.Append(sqlHeader);//?????insert ?
}
}
}
??????????????? ???????????Ч???????????????
????????
????????????????2???????????DataReader??SQL??????????Щ??????????
???????????????????????? ??Ч ?? ????? ?????????????????????????????
?????????????????? ??ν????????? ????????????????????????????????????????Щ??????????????????????д??
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11