using System;
using System.Collections;
using System.Configuration;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
///
///MYSQLHelper 的摘要说明
///
public class MySqlHelper
{
#region 公共属性
///
/// 数据源
///
public string DataSource { get; set; }
///
/// 数据库
///
public string DataBase { get; set; }
///
/// 登录账号
///
public string Uid { get; set; }
///
/// 密码
///
public string Pwd { get; set; }
///
/// 端口号
///
public string Port { get; set; }
///
/// 连接字符串
///
public string ConnStr { get; set; }
MySqlCommand myCmd;
MySqlConnection myConnnect;
#endregion
public MySqlHelper(string dataSource, string dataBase, string uid, string pwd, string port = "3306")
{
DataSource = dataSource;
DataBase = dataBase;
Uid = uid;
Pwd = pwd;
Port = port;
StringBuilder connStr = new StringBuilder();
connStr.Append("Data Source=" + DataSource + ";");
connStr.Append("Database=" + DataBase + ";");
connStr.Append("User Id=" + Uid + ";");
connStr.Append("Password=" + Pwd + ";");
connStr.Append("CharSet=utf8;");
connStr.Append("port=" + Port);
ConnStr = connStr.ToString();
myConnnect = new MySqlConnection(ConnStr);
myConnnect.Open();
//"data source=localhost;uid=root;pwd=123456;database=teaching_database;SslMode = none";
}
///
/// 插入一条数据
///
///
public void Executesql(string cmdText)
{
MySqlCommand myCmd = new MySqlCommand(cmdText, myConnnect);
Console.WriteLine(myCmd.CommandText);
if (myCmd.ExecuteNonQuery() > 0)
{
Console.WriteLine("数据插入成功!");
}
//myCmd.Dispose();
//myConnnect.Close();
}
///
/// 插入多条数据
///
///
public void Executesql(List
{
/*
* MySqlHelper adf = new MySqlHelper("192.168.2.2", "testch", "root", "123456", "3306");
List
SQLStringList.Add("insert into main( number, name,value) values('1234','222','333')");
SQLStringList.Add("insert into main( number, name,value) values('1234','222','444')");
SQLStringList.Add("insert into main( number, name,value) values('1234','222','555')");
SQLStringList.Add("insert into main( number, name,value) values('1234','222','666')");
SQLStringList.Add("insert into main( number, name,value) values('1234','222','777')");
SQLStringList.Add("insert into main( number, name,value) values('1234','222','888')");
SQLStringList.Add("insert into main( number, name,value) values('1234','222','999')");
SQLStringList.Add("insert into main( number, name,value) values('1234','222','aaa')");
SQLStringList.Add("insert into main( number, name,value) values('1234','222','sss')");
SQLStringList.Add("insert into main( number, name,value) values('1234','222','ddd')");
SQLStringList.Add("insert into main( number, name,value) values('1234','222','fff')");
SQLStringList.Add("insert into main( number, name,value) values('1234','222','ggg')");
SQLStringList.Add("insert into main( number, name,value) values('1234','222','hhh')");
SQLStringList.Add("insert into main( number, name,value) values('1234','222','jjj')");
SQLStringList.Add("insert into main( number, name,value) values('1234','222','kkk')");
SQLStringList.Add("insert into main( number, name,value) values('1234','222','zzz')");
SQLStringList.Add("insert into main( number, name,value) values('1234','222','xxx')");
adf.Executesql(SQLStringList);
*/
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = myConnnect;
MySqlTransaction tx = myConnnect.BeginTransaction();
cmd.Transaction = tx;
try
{
for (int n = 0; n < SQLStringList.Count; n++)
{
string strsql = SQLStringList[n].ToString();
if (strsql.Trim().Length > 1)
{
cmd.CommandText = strsql;
cmd.ExecuteNonQuery();
}
//后来加上的
if (n > 0 && (n % 500 == 0 || n == SQLStringList.Count - 1))
{
tx.Commit();
tx = myConnnect.BeginTransaction();
}
}
}
catch (System.Data.SqlClient.SqlException E)
{
tx.Rollback();
throw new Exception(E.Message);
}
}
}