MySQL數(shù)據(jù)庫如何與C#連接-創(chuàng)新互聯(lián)

本篇文章為大家展示了MySQL數(shù)據(jù)庫如何與C#連接,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

創(chuàng)新互聯(lián)建站主要從事成都網(wǎng)站設計、做網(wǎng)站、網(wǎng)頁設計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務。立足成都服務皇姑,十余年網(wǎng)站建設經(jīng)驗,價格優(yōu)惠、服務專業(yè),歡迎來電咨詢建站服務:18980820575

  連接數(shù)據(jù)庫、操作數(shù)據(jù)庫,本質是利用數(shù)據(jù)庫提供的動態(tài)鏈接庫MySql.Data.dll進行操作。MySql.Data.dll提供以下8個類:

  • MySqlConnection: 連接MySQL服務器數(shù)據(jù)庫。
  • MySqlCommand:執(zhí)行一條sql語句。
  • MySqlDataReader: 包含sql語句執(zhí)行的結果,并提供一個方法從結果中閱讀一行。
  • MySqlTransaction: 代表一個SQL事務在一個MySQL數(shù)據(jù)庫。
  • MySqlException: MySQL報錯時返回的Exception。
  • MySqlCommandBuilder: Automatically generates single-table commands used to reconcile changes made to a DataSet with the associated MySQL database.
  • MySqlDataAdapter: Represents a set of data commands and a database connection that are used to fill a data set and update a MySQL database.
  • MySqlHelper: Helper class that makes it easier to work with the provider.
     

1.添加動態(tài)鏈接庫文件

  方法一:Visual Studio,在 項目(右鍵)-管理NuGet程序包(N) 然后在瀏覽里面搜索MySql.Data并進行安裝。

  方法二:安裝數(shù)據(jù)庫MySQL時要選中Connector.NET 6.9的安裝,將C:\Program Files (x86)\MySQL\Connector.NET 6.9\Assemblies里v4.0或v4.5中的MySql.Data.dll添加到項目的引用。v4.0和v4.5,對應Visual Studio具體項目 屬性-應用程序-目標框架 里的.NET Framework的版本號。

2.建立連接(MySqlConnection類)

using MySql.Data.MySqlClient;
String connetStr = "server=127.0.0.1;port=3306;user=root;password=root; database=minecraftdb;";
// server=127.0.0.1/localhost 代表本機,端口號port默認是3306可以不寫
MySqlConnection conn = new MySqlConnection(connetStr);
try
{  
   conn.Open();//打開通道,建立連接,可能出現(xiàn)異常,使用try catch語句
   Console.WriteLine("已經(jīng)建立連接");
   //在這里使用代碼對數(shù)據(jù)庫進行增刪查改
}
catch (MySqlException ex)
{
   Console.WriteLine(ex.Message);
}
finally
{
   conn.Close();
}

網(wǎng)頁名稱:MySQL數(shù)據(jù)庫如何與C#連接-創(chuàng)新互聯(lián)
新聞來源:http://muchs.cn/article42/pdeec.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供自適應網(wǎng)站網(wǎng)站營銷、用戶體驗、品牌網(wǎng)站建設、全網(wǎng)營銷推廣定制開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

外貿網(wǎng)站制作