C # learning database --MySQL basic operations (connection, add, delete, change, check) Package

 EDITORIAL:

 The package SQLserver C #: https://www.cnblogs.com/mexihq/p/11636785.html

 C # Oracle's Package: https://www.cnblogs.com/mexihq/p/11700741.html

In their daily work, usually a large number of items are all the basic operations with the database, so small series of several common database operations packaged into a dll developed to facilitate subsequent use. This article is mainly recorded in C # connection for MySQL, add, delete, change, check the basic operation, and also if there is any problem please enlighten big brother. Follow-up will also have several other common database accordingly finishing. Man of few words said, directly start code code.

Quote:

using MySql.Data.MySqlClient;

  

Declare a MySqlConnection facilitate subsequent use.

private MySqlConnection mysql_con;

 

MySQL open:

/// <summary>
/// MySQL open
/// </summary>
/// <param name="link">link statement</param>
/// <returns>Success:success; Fail:reason</returns>
public string MySQL_Open(string link)
{
  try
  {
    mysql_con = new MySqlConnection(link);
    mysql_con.Open();
    return "success";
  }
  catch (Exception ex)
  {
    return ex.Message;
  }
}

MySQL is closed:

/// <summary>
/// MySQL close
/// </summary>
/// <returns>Success:success Fail:reason</returns>
public string MySQL_Close()
{
  try
  {
    if (mysql_con == null)
    {
      return "No database connection";
    }
    if (mysql_con.State == ConnectionState.Open || mysql_con.State == ConnectionState.Connecting)
    {
      mysql_con.Close();
      mysql_con.Dispose();
    }
    else
    {
      if (mysql_con.State == ConnectionState.Closed)
      {
        return "success";
      }
      if (mysql_con.State == ConnectionState.Broken)
      {
        return "ConnectionState:Broken";
      }
    }
    return "success";
  }
  catch (Exception ex)
  {
    return ex.Message;
  }
}

MySQL additions and deletions:

/// <summary>
/// MySQL insert,delete,update
/// </summary>
/// <param name="sql">insert,delete,update statement</param>
/// <returns>Success:success + Number of affected rows; Fail:reason</returns>
public string MySQL_Insdelupd(string sql)
{
  try
  {
    int num = 0;
    if (mysql_con == null)
    {
      return "Please open the database connection first";
    }
    if (mysql_con.State == ConnectionState.Open)
    {
      MySqlCommand sqlCommand = new MySqlCommand(sql, mysql_con);
      num = sqlCommand.ExecuteNonQuery();
    }
    else
    {
      if (mysql_con.State == ConnectionState.Closed)
      {
        return "Database connection closed";
      }
      if (mysql_con.State == ConnectionState.Broken)
      {
        return "Database connection is destroyed";
      }
      if (mysql_con.State == ConnectionState.Connecting)
      {
        return "The database is in connection";
      }
    }
    return "success" + num;
  }
  catch (Exception ex)
  {
    return ex.Message.ToString();
  }
}

MySQL's investigation:

/// <summary>
/// MySQL select
/// </summary>
/// <param name="sql">select statement</param>
/// <param name="record">Success:success; Fail:reason</param>
/// <returns>select result</returns>
public DataSet MySQL_Select(string sql, out string record)
{
  try
  {
    //储存数据的工具初始化
    DataSet dataSet = new DataSet();
    if (mysql_con == null)
    {
      record = "Please open the database connection first";
      return dataSet;
    }
    if (mysql_con.State == ConnectionState.Open)
    {
      MySqlDataAdapter sqlDataAdapter = new MySqlDataAdapter(sql, mysql_con);
      sqlDataAdapter.Fill(dataSet, "sample");
      sqlDataAdapter.Dispose();
      record = "success";
      return dataSet;
    }
    if (mysql_con.State == ConnectionState.Closed)
    {
      record = "Database connection closed";
      return dataSet;
    }
    if (mysql_con.State == ConnectionState.Broken)
    {
      record = "Database connection is destroyed";
      return dataSet;
    }
    if (mysql_con.State == ConnectionState.Connecting)
    {
      record = "The database is in connection";
      return dataSet;
    }
    record = "ERROR";
    return dataSet;
  }
  catch (Exception ex)
  {
    DataSet dataSet = new DataSet();
    record = ex.Message.ToString();
    return dataSet;
  }
}

 Xiao Bian found that more than this package is still very troublesome way, each time MySQL to perform CRUD must first open the database and, finally, close, practical use up more trouble. Thus a method of the above two CRUD is overloaded, the operations each time the database is opened first, and then close the database.

/// <Summary>
/// INSERT the MySQL, Delete, Update
/// </ Summary>
/// <param name = "SQL"> INSERT, Delete, Update Statement </ param>
/// <param name = "Link"> Link Statement </ param>
/// <Returns> Success: Success + Number The rows of affected; the Fail: reason </ Returns>
public String MySQL_Insdelupd (SQL String, String Link)
{
  the try
  {
    int NUM = 0;
    the using (the MySqlConnection the MySqlConnection new new = CON (link))
    {
      con.Open ();
      tools // database operation the SqlCommand
      the MySqlCommand the MySqlCommand new new cmd = (SQL, CON); // (action statement and linking tools)
      NUM = the cmd.ExecuteNonQuery (); // perform the operation returns to affect the number of rows
      con.Close ();
      return "success" + num;
    }
  }
  catch (Exception ex)
  {
    return ex.Message.ToString();
  }
}

 

/// <Summary>
/// the MySQL SELECT
/// </ Summary>
/// <param name = "SQL"> SELECT Statement </ param>
/// <param name = "Link"> Link Statement </ param>
/// <param name = "Record"> Success: Success; the Fail: reason </ param>
/// <Returns> SELECT Result </ Returns>
public MySQL_Select the DataSet (SQL String, String Link, Record OUT String)
{
  the try
  {
    // initialization means for storing data
    the DataSet the DataSet new new DS = ();
    // tools corresponds to a link to a database (connection string)
    the using (the MySqlConnection the MySqlConnection new new = CON (link))
    {
      con.Open () ; // open
      // SqlConnection tool with links to databases, is now stored in sql adapter by sql query results
      MySqlDataAdapter sda = new MySqlDataAdapter (sql, con); // ( query and connection means)
      sda.Fill (DS, "Sample"); // data into the adapter DataSet tool
      con.Close (); // with After the SqlConnection tool
      sda.Dispose (); // manual release the SqlDataAdapter
      Record = "Success";
      return DS;
    }
  }
  the catch (Exception EX)
  {
    the DataSet the DataSet new new dataSet A = ();
    Record ex.Message.ToString = ();
    dataSet A return;
  }
}

 

Guess you like

Origin www.cnblogs.com/mexihq/p/12463423.html