茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1672051 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
F09_407.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

Dot Net? : [轉貼]如何使用 C# 存取 MySQL 資料庫

發表者 討論內容
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]如何使用 C# 存取 MySQL 資料庫

[教學] 如何使用C#存取MySQL資料庫

今天本魔要講講如何使用C#存取MySQL資料庫

MySQL是一個open source的關聯性資料庫管理系統

可能會有人有疑問說:「資料庫和機器人怎麼有會關聯呢?」

本魔在此告訴你!一定有關聯的!

因為關聯這種東西就是讓我們扯出來的!

在網路上的資料庫可以隨時供電腦做存取

這樣一來,我們就可以很方便的管理一些資料了

也有可能可以把動作資料建立在資料庫上供機器人使用(好像很久以前有人就有給過我這個建議了)

這樣除了較不用擔心資料會隨著機器人損壞

更增加了許多的擴充性




那麼本魔在此就不教學如何安裝MySQL了

各位自己上網Google應該很好找到

那麼就開始我們的教學!

首先各位需要安裝 MySQL官方所提供的Connector

安裝好了之後就可以開啟我們的Visual Studio建一個C#的專案囉!

首先要加入參考    [Project] ->[Add Reference]->[瀏覽]

選擇安裝路徑下的MySql.Data.dll




路徑通常在C:\Program Files\MySQL\MySQL Connector Net 6.6.5\Assemblies\v2.0

加入參考了之後會看到方案總管內的Reference有加入了MySql.Data囉


接了我們就可以開始撰寫程式囉

以下給各位新增、刪除、修改、查詢的範例

其實相當簡單

基本上就是與MySQL一樣   丟一串指令給他   他會回傳一些資料給你

首先假設我們的資料表的內容是這樣子的而資料表名稱為member


那我們先來新增10筆會員資料



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
namespace MySQLtest
{
class Program
{
static void Main(string[] args)
{
string dbHost = "";//資料庫位址
string dbUser = "";//資料庫使用者帳號
string dbPass = "";//資料庫使用者密碼
string dbName = "";//資料庫名稱
string connStr = "server=" + dbHost + ";uid=" + dbUser + ";pwd=" + dbPass + ";database=" + dbName;
MySqlConnection conn = new MySqlConnection(connStr);
MySqlCommand command = conn.CreateCommand();
conn.Open();
String account;
String password;
int level;
for(int i = 0; i < 10; i++){
account = "account" + i.ToString();
password = "password" + i.ToString();
level = i * 10;
command.CommandText = "Insert into member(account,password,level) values('"+ account +"','"+ password +"',"+ level +")";
command.ExecuteNonQuery();
}
Console.ReadLine();
conn.Close();
}
}
}

這樣執行後我們就可以看到資料表內增加了十筆資料囉

接著是刪除

假如我想刪除id為15的用戶


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
namespace MySQLtest
{
class Program
{
static void Main(string[] args)
{ string dbHost = "";//資料庫位址
string dbUser = "";//資料庫使用者帳號
string dbPass = "";//資料庫使用者密碼
string dbName = "";//資料庫名稱
string connStr = "server=" + dbHost + ";uid=" + dbUser + ";pwd=" + dbPass + ";database=" + dbName;
MySqlConnection conn = new MySqlConnection(connStr);
MySqlCommand command = conn.CreateCommand();
conn.Open();
command.CommandText = "Delete FROM member WHERE id=15";
int n = command.ExecuteNonQuery();
Console.WriteLine("共刪除 {0} 筆資料", n);
Console.ReadLine();
conn.Close();
}
}
}


那麼執行後就可以發現id為15的資料不見囉


接著是修改

這裡把account6的password改為1234


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
namespace MySQLtest
{
class Program
{
static void Main(string[] args)
{ string dbHost = "";//資料庫位址
string dbUser = "";//資料庫使用者帳號
string dbPass = "";//資料庫使用者密碼
string dbName = "";//資料庫名稱
string connStr = "server=" + dbHost + ";uid=" + dbUser + ";pwd=" + dbPass + ";database=" + dbName;
MySqlConnection conn = new MySqlConnection(connStr);
MySqlCommand command = conn.CreateCommand();
conn.Open();
command.CommandText = "Update member SET password='1234' WHERE account='account6'";
command.ExecuteNonQuery();
Console.ReadLine();
conn.Close();
}
}
}


再來看看資料表   沒錯   正確的改過去了



最後就是查詢啦

那我們來查詢看看level小於50的所有用戶

然後顯示在電腦上吧


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
namespace MySQLtest
{
class Program
{
static void Main(string[] args)
{ string dbHost = "";//資料庫位址
string dbUser = "";//資料庫使用者帳號
string dbPass = "";//資料庫使用者密碼
string dbName = "";//資料庫名稱
string connStr = "server=" + dbHost + ";uid=" + dbUser + ";pwd=" + dbPass + ";database=" + dbName;
MySqlConnection conn = new MySqlConnection(connStr);
MySqlCommand command = conn.CreateCommand();
conn.Open();
String cmdText = "SELECT * FROM member WHERE level < 50";
MySqlCommand cmd = new MySqlCommand(cmdText, conn);
MySqlDataReader reader = cmd.ExecuteReader(); //execure the reader
while (reader.Read())
{
for (int i = 0; i < 4; i++)
{
String s = reader.GetString(i);
Console.Write(s + "\t");
}
Console.Write("\n");
}
Console.ReadLine();
conn.Close();
}
}
}




最後結果是


正確!

原文出處:魔人日誌: [教學] 如何使用C#存取MySQL資料庫
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]C# 連接 MySQL

[更新] C#連接MySQL

我在五年前(好久遠…)寫過一篇關於 C# 連接 MySQL的文章,由於年代久遠MySQL也更新了相關的DLL,所以舊的程式會發生一些錯誤,因此我更新了這段程式,希望對於有使用C#連接到MySQL的朋友有所幫助。

安裝MySQL Connector
1. 首先請先到MySQL官方網站下載新的Connector: Connector/Net 6.2
2. 安裝完畢之後請開啟你的Visual Studio,點選專案名稱後按右鍵,接著點選Add Reference
MySQL Connector Add Reference

3. 按下Add Reference之後會開啟下面的視窗,切換到Browse頁籤,如果剛剛安裝Connector的時候是使用預設的路徑,那麼請切換到 C:\Program Files\MySQL\MySQL Connector Net 6.2.1\Assemblies
這個路徑,就可以看到跟我截圖一樣的畫面。
Add Reference MySQL Connector Step 2

4. 根據你的需求選擇要加入的DLL,選好之後按下OK,可以在Solution Explorer看到MySql.Data
Add Reference MySQL Connector Step 4

撰寫程式

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace mysql
{
class Program
{
static void Main(string[] args)
{
string dbHost = "資料庫位址";
string dbUser = "資料庫使用者名稱";
string dbPass = "資料庫使用者密碼";
string dbName = "資料庫名稱";


// 如果有特殊的編碼在database後面請加上;CharSet=編碼, utf8請使用utf8_general_ci
string connStr = "server="+dbHost+";uid="+dbUser+";pwd="+dbPass+";database="+dbName;
MySqlConnection conn = new MySqlConnection(connStr);

// 連線到資料庫
try
{
conn.Open();
}
catch (MySql.Data.MySqlClient.MySqlException ex) {
switch (ex.Number)
{
case 0:
Console.WriteLine("無法連線到資料庫.");
break;
case 1045:
Console.WriteLine("使用者帳號或密碼錯誤,請再試一次.");
break;
}
}


// 進行select
string SQL = "select plain from yammer order by id desc limit 0,10 ";
try
{
MySqlCommand cmd = new MySqlCommand(SQL, conn);
MySqlDataReader myData = cmd.ExecuteReader();


if (!myData.HasRows)
{
// 如果沒有資料,顯示沒有資料的訊息
Console.WriteLine("No data.");
}
else
{
// 讀取資料並且顯示出來
while (myData.Read())
{
Console.WriteLine("Text={0}", myData.GetString(0));
}
myData.Close();
}
}
catch (MySql.Data.MySqlClient.MySqlException ex) {
Console.WriteLine("Error " + ex.Number + " : " + ex.Message);
}
}
}
}

執行結果,中文也可以正常顯示
C# connect to MySQL Result

如果有任何問題歡迎跟我一起討論


原文出處:[更新] C#連接MySQL 阿維雜記本 (Wei's Blog)
前一個主題 | 下一個主題 | 頁首 | | |



Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|