VB.NETWindow視窗版mySQL資料庫存取關鍵程式碼(mySqlClient連線方法)

目錄http://www.cnblogs.com/morrispan/archive/2010/10/23/1859238.html#_Toc229935173

 

13-1.觀念:VB.NET要先安裝Connector/Net套件,才能讀取mySQL:

先到www.mysql.org 下載Connector/Net套件

2009-05-08

的最新版本是

Connector/Net 6.0

 

http://dev.mysql.com/downloads/connector/net/6.0.html

注意:之後也會有更新的版本,請自行下載當時最新的版本

連結至http://dev.mysql.com/downloads/connector/net/6.0.html

下載Connector/Net 6.0組件安裝程式,

下載檔案名稱預設為mysql-connector-net-6.0.3.zip

clip_image002

clip_image004

http://dev.mysql.com/get/Downloads/Connector-Net/mysql-connector-net-6.0.3.zip/from/http://mysql.cs.pu.edu.tw/

mysql-connector-net-6.0.3.zip

執行mysql.data.msi檔案

clip_image006

執行MySql.Data.msi安裝程式,依提示完成安裝步驟。

clip_image008

安裝完成後,

在MS Visual Web Deleloper 新建一個專案,然後加入參考MySql.Data.dll

新增一個視窗/或Web專案,並加入參考MySql.Data.dll

此組件預設安裝置於

C:\Program Files\MySQL\MySQL Connector Net 6.0.3\Binaries\.NET 2.0\MySql.Data.dll

clip_image010

clip_image012

如果程式碼是在個別檔案中的話,在程式的最前面,要引入適當的NameSpace:Imports MySql.Data.MySqlClient

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

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

 

13-2.觀念:C#.NET要先安裝Connector/Net套件,才能寫讀取mySQL的網頁

若是使用C#.NET來撰寫網頁程式,可以用以下方法

(1).安裝: Install MySQL Connector NET

連結至http://dev.mysql.com/downloads/connector/net/6.0.html

下載Connector/Net 6.0組件安裝程式,

下載檔案名稱預設為mysql-connector-net-6.0.3.zip

clip_image002[1]

clip_image004[1]

http://dev.mysql.com/get/Downloads/Connector-Net/mysql-connector-net-6.0.3.zip/from/http://mysql.cs.pu.edu.tw/

mysql-connector-net-6.0.3.zip

執行mysql.data.msi檔案

clip_image006[1]

執行MySql.Data.msi安裝程式,依提示完成安裝步驟。

clip_image008[1]

clip_image014

clip_image016

(2).Copy MySql.Data.dll

路徑:

C:\Program Files\MySQL\MySQL Connector Net 6.0.3\Binaries\.NET 2.0\MySql.Data.dll

copy到 「網頁目錄\bin\」之下

(3).修改C#程式碼

原本是用mssql

現在把程式碼using部份改成

//using System.Data.SqlClient;

using MySql.Data.MySqlClient;

SqlClient相關語法前面改加上My

//SqlDataAdapter

MySqlDataAdapter

//SqlCommand

MySqlCommand

//連線字串改成以下

string constr = "User Id=root;Host=localhost;Database

=qing;password=qing";

MySqlConnection mycn = new MySqlConnection(constr);

(4).參考dll

編譯時參考「MySql.Data.dll」

/r:bin\MySql.Data.dll

clip_image010[1]

clip_image012[1]

string constr = "User Id=root;Host=localhost;Database

=qing;password=qing";

MySqlConnection mycn = new MySqlConnection(constr);

 

13-3.顯示全部資料表內容的關鍵程式碼

Imports MySql.Data.MySqlClient

'連結 mySQL 資料庫

Dim str As String = "Server=localhost;database=資料庫名稱;User Id=帳戶名稱;Password=密碼;pooling=false;"

'Dim str As String = "Server=(local);database=資料庫名稱;User Id=帳戶名稱;Password=密碼;pooling=false;"

'Dim str As String = "Server=伺服器名稱SQLexpress;database=資料庫名稱;User Id=帳戶名稱;Password=密碼;pooling=false;"

Dim conn As MySqlConnection = New MySqlConnection (str)

conn.Open()

'查詢資料

Dim str1 As String = "select * from 資料表"

Dim adp1 As MySqlDataAdapter = New MySqlDataAdapter (str1, conn)

'將查詢結果放到記憶體set1上的"1a "表格內

Dim set1 As DataSet = New DataSet

adp1.Fill(set1, "1a")

'將記憶體的資料集合存放到視窗畫面上的DataGrid上

DataGridView1.DataSource = set1.Tables("1a")

'關閉資料庫的連結

conn.Close()

 

13-4.查詢『某紀錄』的關鍵程式碼

Imports MySql.Data.MySqlClient

'連結 mySQL 資料庫

Dim str As String = "Server=localhost;database=資料庫名稱;User Id=帳戶名稱;Password=密碼;pooling=false;"

'Dim str As String = "Server=(local);database=資料庫名稱;User Id=帳戶名稱;Password=密碼;pooling=false;"

'Dim str As String = "Server=伺服器名稱SQLexpress;database=資料庫名稱;User Id=帳戶名稱;Password=密碼;pooling=false;"

Dim conn As SqlConnection = New SqlConnection(str)

conn.Open()

'查詢資料

Dim str1 As String = " Select * from 資料表 where name = 'jack'"

Dim adp1 As SqlDataAdapter = New SqlDataAdapter(str1, conn)

'將查詢結果放到記憶體set1上的"1a "表格內

Dim set1 As DataSet = New DataSet

adp1.Fill(set1, "1a")

'將記憶體的資料集合存放到視窗畫面上的DataGridView上

DataGridView1.DataSource = set1.Tables("1a")

'關閉資料庫的連結

conn.Close()

注意

查詢
(a).SQL查詢『某紀錄』語法
n Select * from 1a where name = ‘jack’
(b).當有變數時的SQL查詢語法(C#)
n “Select * from 1a where name = ‘” + textBox1.text + “’”
(c).當有變數時的SQL查詢語法(VB.NET)
n “Select * from 1a where name = ‘” & textBox1.text & “’”

 

13-5.以關鍵字的方式查詢『某紀錄』的關鍵程式碼

Imports MySql.Data.MySqlClient

'連結 mySQL 資料庫

Dim str As String = "Server=localhost;database=資料庫名稱;User Id=帳戶名稱;Password=密碼;pooling=false;"

'Dim str As String = "Server=(local);database=資料庫名稱;User Id=帳戶名稱;Password=密碼;pooling=false;"

'Dim str As String = "Server=伺服器名稱SQLexpress;database=資料庫名稱;User Id=帳戶名稱;Password=密碼;pooling=false;"

Dim conn As SqlConnection = New SqlConnection(str)

conn.Open()

'查詢資料

Dim str1 As String = " Select * from 資料表 where name like '%jack%'"

Dim adp1 As SqlDataAdapter = New SqlDataAdapter(str1, conn)

'將查詢結果放到記憶體set1上的"1a "表格內

Dim set1 As DataSet = New DataSet

adp1.Fill(set1, "1a")

'將記憶體的資料集合存放到視窗畫面上的DataGrid上

DataGridView1.DataSource = set1.Tables("1a")

'關閉資料庫的連結

conn.Close()

注意

查詢
(a).SQL查詢『某紀錄』語法
n Select * from 1a where name like ‘%jack%’
(b).當有變數時的SQL查詢語法(C#)
n “Select * from 1a where name like ‘%” + textBox1.text + “%’”
(c).當有變數時的SQL查詢語法(VB.NET)
n “Select * from 1a where name like ‘%” & textBox1.text & “%’”

 

13-6.刪除『某紀錄』的關鍵程式碼

Imports MySql.Data.MySqlClient

'連結 mySQL 資料庫

Dim str As String = "Server=localhost;database=資料庫名稱;User Id=帳戶名稱;Password=密碼;pooling=false;"

'Dim str As String = "Server=(local);database=資料庫名稱;User Id=帳戶名稱;Password=密碼;pooling=false;"

'Dim str As String = "Server=伺服器名稱SQLexpress;database=資料庫名稱;User Id=帳戶名稱;Password=密碼;pooling=false;"

Dim conn As SqlConnection = New SqlConnection(str)

conn.Open()

'刪除資料庫內的記錄

'設定刪除記錄的 SQL語法 及資料庫執行指令OleDbCommand

Dim str1 As String = "delete * from 資料表 where name = 'jack'"

Dim cmd As SqlCommand = New SqlCommand(str1, conn)

'執行資料庫指令SqlCommand

cmd.ExecuteNonQuery()

'關閉資料庫的連結

conn.Close()

'顯示成功刪除記錄的訊息()

MessageBox.Show("成績刪除一筆記錄了", "成功刪除", MessageBoxButtons.OKCancel,MessageBoxIcon.Information)

刪除
(a).SQL刪除『某紀錄』語法
n delete * from 1a where name = ‘jack’
(b).當有變數時的SQL刪除詢語法(C#)
n “delete * from 1a where name = ‘” + textBox1.text + “’”
(c).當有變數時的SQL刪除語法(VB.NET)
n “delete * from 1a where name = ‘” & textBox1.text & “’”

 

13-7.新增『某紀錄』的關鍵程式碼

Imports MySql.Data.MySqlClient

'連結 mySQL 資料庫

Dim str As String = "Server=localhost;database=資料庫名稱;User Id=帳戶名稱;Password=密碼;pooling=false;"

'Dim str As String = "Server=(local);database=資料庫名稱;User Id=帳戶名稱;Password=密碼;pooling=false;"

'Dim str As String = "Server=伺服器名稱SQLexpress;database=資料庫名稱;User Id=帳戶名稱;Password=密碼;pooling=false;"

Dim conn As SqlConnection = New SqlConnection(str)

conn.Open()

'新增記錄到資料庫內

'設定新增記錄的 SQL語法 及資料庫執行指令SqlCommand

Dim str1 As String = "Insert Into 資料表(name,chi)Values('jack',90)"

Dim cmd As SqlCommand = New SqlCommand(str1, conn)

'執行資料庫指令SqlCommand

cmd.ExecuteNonQuery()

'關閉資料庫的連結

conn.Close()

'顯示成功新增記錄的訊息()

MessageBox.Show("成績新增一筆記錄了", "成功新增", MessageBoxButtons.OKCancel,MessageBoxIcon.Information)

新增
(a).SQL新增『某紀錄』語法
n Insert Into 1a(name,chi)Values(‘jack’,90)
(b).當有變數時的SQL新增語法(C#)
n “Insert Into 1a(name,chi)Values ‘” + textBoxName.text + “’,” + Int32.Parse(textBoxName.text) + “)”;
(c).當有變數時的SQL新增語法(VB.NET)
n “Insert Into 1a(name,chi)Values ‘” & textBoxName.text & “’,” & Int32.Parse(textBoxName.text) & “)”;”

 

13-8.修改『某紀錄』的關鍵程式碼

Imports MySql.Data.MySqlClient

'連結 mySQL 資料庫

Dim str As String = "Server=localhost;database=資料庫名稱;User Id=帳戶名稱;Password=密碼;pooling=false;"

'Dim str As String = "Server=(local);database=資料庫名稱;User Id=帳戶名稱;Password=密碼;pooling=false;"

'Dim str As String = "Server=伺服器名稱SQLexpress;database=資料庫名稱;User Id=帳戶名稱;Password=密碼;pooling=false;"

Dim conn As SqlConnection = New SqlConnection(str)

conn.Open()

'修改資料庫內的記錄

' 設定修改記錄的 SQL語法及資料庫執行指令SqlCommand

Dim str1 As String = "Update 資料表 set name = 'jack', chi = 90 where id_no='90001'"

Dim cmd As SqlCommand = New SqlCommand(str1, conn)

'執行資料庫指令SqlCommand

cmd.ExecuteNonQuery()

'關閉資料庫的連結

conn.Close()

'顯示成功修改記錄的訊息()

MessageBox.Show("成績修改一筆記錄了", "成功修改", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)

修改
(a).SQL修改『某紀錄』語法
n Update 1a set name = jack, chi = 90 where id_no=90001
(b).當有變數時的SQL修改語法(C#)
n “Update 1a set name = '" + textBoxName.Text + "', chi = " + Int32.Parse(textBoxChi.Text) + " where id_no=' " + textBoxNo.Text + "'";
B.注意2:什麼時候要用單引號『’』,什麼時候不用加呢

n 字串: 字串前後要加上 ‘ 單引號

Values('" & txt_5_PrjCode.Text & "')”

n 數值:數值前後要不用加,但是所引用的textbox.text要先用Int32.Parse轉換成數值格式

Values(" & Int32.Parse (txt_5_Period.Text) & "')”

n 日期:字串前後要加上 ‘ 單引號,所引用的textbox.text要先用DateTime.Parse轉換成日期格式

Values('" & DateTime.Parse(txt_5_BeginDay.Text) & "')”

arrow
arrow
    全站熱搜

    JL8051 發表在 痞客邦 留言(0) 人氣()