Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

2011年5月22日 星期日

C# MySQL

利用 C# 在 Windows Application 和 Device Application

寫存取資料庫的程式

語法一樣

但是要加入的參考卻不一樣

對視窗應用程式而言,要加入 MySql.Data.dll 的參考

對裝置應用程式而言,則要加入 MySql.Data.CF.dll 的參考

另外,兩者均要使用命名空間

也就是 using MySql.Data.MySqlClient;

接下來是資料庫資料取得的大致語法

(這邊只有從資料庫取資料出來而已)


//準備好連線對象
string connection_string = "SERVER=伺服器位址;DATABASE=資料庫;UID=帳號;PASSWORD=密碼;";

//宣告連線物件
MySqlConnection connection = new MySqlConnection(connection_string);

//打開連線
connection.Open();

//由於需要SQL語法,因此宣告出命令物件
MySqlCommand command=connection.CreateCommand();

//準備好要執行的SQL語法
string commandstring = "SQL語法";

//將語法指定給命令物件
command.CommandText=commandstring;

//另宣告物件以為讀取之用
MySqlDataReader reader = command.ExecuteReader();

//另宣告一個變數用來接資料庫讀到的資料
string result = "";

//看要做什麼事直到讀完
while(reader.Read())
{
//GetString(0)可以得到index為0的資料,依此類推
result = reader.GetString(0);
}

//關閉連線
reader.Close();
connection.Close();


http://fantasymew.pixnet.net/blog/post/23939881
http://blog.hsdn.net/117.html

沒有留言:

張貼留言