Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Data;
Using System.Text.RegularExpressions;
Using MySql.Data.MySqlClient;
namespace MySQL
{
Class Mysqlconnection
{
Mysqlconnection mysqlconnection;
DataSet DataSet;
String IP = "192.168.0.101";
String UserName = "root";
String Password = "123456";
String Database = "GA";
<summary>
Establish a MySQL connection
</summary>
Public Mysqlconnection ()
{
Try
{
Mysqlconnection = new Mysqlconnection ("datasource=" + IP + "Username=" + username + ";p assword=" + password + ";d atabase= "+ Database +"; Charset=utf8 ");
}
catch (Mysqlexception ex)
{
Console.WriteLine (ex. Message);
}
}
Public Mysqlconnection (String IP, String UserName, String Password, String Database)
{
Try
{
String connectionString = "Datasource=" + IP + "Username=" + username + ";p assword=" + password + ";d atabase=" + Database + "; charset=gb2312";
Mysqlconnection = new Mysqlconnection (connectionString);
}
catch (Mysqlexception ex)
{
Console.WriteLine (ex. Message);
}
}
public string Mysqlinfo ()
{
string mysqlinfo = null;
Try
{
Mysqlconnection.open ();
Mysqlinfo + = "Connection opened." + Environment.NewLine;
Mysqlinfo + = "Connection String:" + mysqlConnection.ConnectionString.ToString () + Environment.NewLine;
Mysqlinfo + = "Database:" + mysqlConnection.Database.ToString () + Environment.NewLine;
Mysqlinfo + = "Connection serverversion:" + mysqlConnection.ServerVersion.ToString () + Environment.NewLine;
Mysqlinfo + = "Connection State:" + mysqlConnection.State.ToString () + Environment.NewLine;
}
catch (Mysqlexception ex)
{
Console.WriteLine ("Mysqlexception Error:" + ex.) ToString ());
}
Finally
{
Mysqlconnection.close ();
}
return mysqlinfo;
}
<summary>
Execute SQL statement no return result
</summary>
public int Mysqlcommand (string mysqlcommand)
{
Try
{
Mysqlconnection.open ();
Console.WriteLine ("Mysqlconnection opened.");
Mysqlcommand Mysqlcommand = new Mysqlcommand (Mysqlcommand, mysqlconnection);
return Mysqlcommand.executenonquery ();
}
catch (Mysqlexception ex)
{
Console.WriteLine ("Mysqlexception Error:" + ex.) ToString ());
if (Regex.IsMatch (ex. ToString (), ""))
{
Console.WriteLine (ex. Message);
}
}
Finally
{
Mysqlconnection.close ();
}
return-1;
}
<summary>
Execute SELECT statement returns execution result
</summary>
Public DataView mysqldataadapter (string table)
{
DataView DataView = new DataView ();
Try
{
Mysqlconnection.open ();
Mysqldataadapter mysqldataadapter = new Mysqldataadapter ("SELECT * from" + table, mysqlconnection);
DataSet = new DataSet ();
Mysqldataadapter.fill (dataSet, table);
DataView = dataset.tables[table]. DefaultView;
}
catch (Mysqlexception ex)
{
Console.WriteLine (ex. Message);
}
Finally
{
Mysqlconnection.close ();
}
return dataView;
}
<summary>
Statistics record number parameter: SELECT COUNT (*) from Isns_users
</summary>
Public long Mysqlcountrow (String sql)
{
DataView DataView = new DataView ();
Try
{
Mysqlconnection.open ();
Mysqlcommand mycm = new Mysqlcommand (sql, mysqlconnection);
Mysqldatareader MSDR = mycm. ExecuteReader ();
Long RecordCount = (long) mycm. ExecuteScalar ();
return recordCount;
}
catch (Mysqlexception)
{
return-1;
Console.WriteLine (ex. Message);
}
Finally
{
Mysqlconnection.close ();
}
return 0;
}
}//end class
}
C # Operation MySQL class