usingSystem;usingSystem.Configuration;usingSystem.Collections;usingSystem.Data;usingMySql.Data;usingMySql.Data.MySqlClient;namespaceMyData
{///
///通用数据库类MySQL///
public classClass_mysql_conn
{//public static string ConnStr = @"server=数据库;uid=帐号;pwd=密码;database=数据库;charset=utf8";
public static string ConnStr = MyData.Properties.Settings.Default.my_soft_mysqlConn + "pwd=密码;charset=utf8;";//打开数据库链接
public static MySqlConnection Open_Conn( stringConnStr)
{
MySqlConnection Conn= newMySqlConnection(ConnStr);
Conn.Open();returnConn;
}//关闭数据库链接
public static voidClose_Conn(MySqlConnection Conn)
{if(Conn != null)
{
Conn.Close();
Conn.Dispose();
}
GC.Collect();
}//运行MySql语句
public static int Run_SQL(string SQL, stringConnStr)
{
MySqlConnection Conn=Open_Conn(ConnStr);
MySqlCommand Cmd=Create_Cmd(SQL, Conn);try{int result_count =Cmd.ExecuteNonQuery();
Close_Conn(Conn);returnresult_count;
}catch{
Close_Conn(Conn);return 0;
}
}//生成Command对象
public static MySqlCommand Create_Cmd(stringSQL, MySqlConnection Conn)
{
MySqlCommand Cmd= newMySqlCommand(SQL, Conn);returnCmd;
}//运行MySql语句返回 DataTable
public static DataTable Get_DataTable(string SQL, string ConnStr, stringTable_name)
{
MySqlDataAdapter Da=Get_Adapter(SQL, ConnStr);
DataTable dt= newDataTable(Table_name);
Da.Fill(dt);returndt;
}//运行MySql语句返回 MySqlDataReader对象
public static MySqlDataReader Get_Reader(string SQL, stringConnStr)
{
MySqlConnection Conn=Open_Conn(ConnStr);
MySqlCommand Cmd=Create_Cmd(SQL, Conn);
MySqlDataReader Dr;try{
Dr=Cmd.ExecuteReader(CommandBehavior.Default);
}catch{throw newException(SQL);
}
Close_Conn(Conn);returnDr;
}//运行MySql语句返回 MySqlDataAdapter对象
public static MySqlDataAdapter Get_Adapter(string SQL, stringConnStr)
{
MySqlConnection Conn=Open_Conn(ConnStr);
MySqlDataAdapter Da= newMySqlDataAdapter(SQL, Conn);returnDa;
}//运行MySql语句,返回DataSet对象
public static DataSet Get_DataSet(string SQL, stringConnStr, DataSet Ds)
{
MySqlDataAdapter Da=Get_Adapter(SQL, ConnStr);try{
Da.Fill(Ds);
}catch(Exception Err)
{throwErr;
}returnDs;
}//运行MySql语句,返回DataSet对象
public static DataSet Get_DataSet(string SQL, string ConnStr, DataSet Ds, stringtablename)
{
MySqlDataAdapter Da=Get_Adapter(SQL, ConnStr);try{
Da.Fill(Ds,tablename);
}catch(Exception Ex)
{throwEx;
}returnDs;
}//运行MySql语句,返回DataSet对象,将数据进行了分页
public static DataSet Get_DataSet(string SQL, string ConnStr, DataSet Ds, int StartIndex, int PageSize, stringtablename)
{
MySqlConnection Conn=Open_Conn(ConnStr);
MySqlDataAdapter Da=Get_Adapter(SQL, ConnStr);try{
Da.Fill(Ds, StartIndex, PageSize, tablename);
}catch(Exception Ex)
{throwEx;
}
Close_Conn(Conn);returnDs;
}//返回MySql语句执行结果的第一行第一列
public static string Get_Row1_Col1_Value(string SQL, stringConnStr)
{
MySqlConnection Conn=Open_Conn(ConnStr);stringresult;
MySqlDataReader Dr ;try{
Dr=Create_Cmd(SQL,Conn).ExecuteReader();if(Dr.Read())
{
result= Dr[0].ToString();
Dr.Close();
}else{
result= "";
Dr.Close();
}
}catch{throw newException(SQL);
}
Close_Conn(Conn);returnresult;
}
}
}