程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#銜接mysql數據庫完全實例

C#銜接mysql數據庫完全實例

編輯:C#入門知識

C#銜接mysql數據庫完全實例。本站提示廣大學習愛好者:(C#銜接mysql數據庫完全實例)文章只能為提供參考,不一定能成為您想要的結果。以下是C#銜接mysql數據庫完全實例正文


本文實例講述了C#銜接mysql數據庫的辦法。分享給年夜家供年夜家參考。詳細完成辦法以下:

using System;
using System.Configuration;
using MySql.Data.MySqlClient;
/// <summary>
/// TestDatebase 的摘要解釋
/// </summary>
public class TestDatebase
{
  public TestDatebase()
  {
    //
    // TODO: 在此處添加結構函數邏輯
    //
  }
  public static void Main(String[] args)
  {
    MySqlConnection mysql = getMySqlCon();
    //查詢sql
    String sqlSearch = "select * from student";
    //拔出sql
    String sqlInsert = "insert into student values (12,'張三',25,'年夜專')";
    //修正sql
    String sqlUpdate = "update student set name='李四' where id= 3";
    //刪除sql
    String sqlDel = "delete from student where id = 12";
    //打印SQL語句
    Console.WriteLine(sqlDel);
    //四種語句對象
    //MySqlCommand mySqlCommand = getSqlCommand(sqlSearch, mysql);
    //MySqlCommand mySqlCommand = getSqlCommand(sqlInsert, mysql);
    //MySqlCommand mySqlCommand = getSqlCommand(sqlUpdate, mysql);
    MySqlCommand mySqlCommand = getSqlCommand(sqlDel, mysql);
    mysql.Open();
    //getResultset(mySqlCommand);
    //getInsert(mySqlCommand);
    //getUpdate(mySqlCommand);
    getDel(mySqlCommand);
    //記得封閉
    mysql.Close();
    String readLine = Console.ReadLine();
  }
  /// <summary>
  /// 樹立mysql數據庫鏈接
  /// </summary>
  /// <returns></returns>
  public static MySqlConnection getMySqlCon()
  {
    String mysqlStr = "Database=test;Data Source=127.0.0.1;User Id=root;Password=root;pooling=false;CharSet=utf8;port=3306";
    // String mySqlCon = ConfigurationManager.ConnectionStrings["MySqlCon"].ConnectionString;
    MySqlConnection mysql = new MySqlConnection(mysqlStr);
    return mysql;
  }
  /// <summary>
  /// 樹立履行敕令語句對象
  /// </summary>
  /// <param name="sql"></param>
  /// <param name="mysql"></param>
  /// <returns></returns>
  public static MySqlCommand getSqlCommand(String sql,MySqlConnection mysql)
  {
    MySqlCommand mySqlCommand = new MySqlCommand(sql, mysql);
    // MySqlCommand mySqlCommand = new MySqlCommand(sql);
    // mySqlCommand.Connection = mysql;
    return mySqlCommand;
  }
  /// <summary>
  /// 查詢並取得成果集並遍歷
  /// </summary>
  /// <param name="mySqlCommand"></param>
  public static void getResultset(MySqlCommand mySqlCommand)
  {
    MySqlDataReader reader = mySqlCommand.ExecuteReader();
    try
    {
      while (reader.Read())
      {
        if (reader.HasRows)
        {
          Console.WriteLine("編號:" + reader.GetInt32(0) + "|姓名:" + reader.GetString(1) + "|年紀:" + reader.GetInt32(2) + "|學歷:" + reader.GetString(3));
        }
      }
    }
    catch (Exception)
    {
      Console.WriteLine("查詢掉敗了!");
    }
    finally
    {
      reader.Close();
    }
  }
  /// <summary>
  /// 添加數據
  /// </summary>
  /// <param name="mySqlCommand"></param>
  public static void getInsert(MySqlCommand mySqlCommand)
  {
    try
    {
      mySqlCommand.ExecuteNonQuery();
    }
    catch (Exception ex)
    {
      String message = ex.Message;
      Console.WriteLine("拔出數據掉敗了!" + message);
    }
   
  }
  /// <summary>
  /// 修正數據
  /// </summary>
  /// <param name="mySqlCommand"></param>
  public static void getUpdate(MySqlCommand mySqlCommand)
  {
    try
    {
      mySqlCommand.ExecuteNonQuery();
    }
    catch (Exception ex)
    {
      String message = ex.Message;
      Console.WriteLine("修正數據掉敗了!" + message);
    }
  }
  /// <summary>
  /// 刪除數據
  /// </summary>
  /// <param name="mySqlCommand"></param>
  public static void getDel(MySqlCommand mySqlCommand)
  {
    try
    {
      mySqlCommand.ExecuteNonQuery();
    }
    catch (Exception ex)
    {
      String message = ex.Message;
      Console.WriteLine("刪除數據掉敗了!" + message);
    }
  }
}

願望本文所述對年夜家的C#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved