C # Operation MySQL class

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.