因為之前開發系統的資料都放在 mySQL 的資料庫,為了讀取 mySQL 的資料也花了一番功夫。
1. 首先,得先下載並安裝 MySQL Connector Net
2. Add Reference
MySql.Data.MySqlClient
System.Data.Odbc
3. 加入 Name Space
<%@ import namespace="System.Data" %>
<%@ import namespace="System.Data.MysqlClient" %>
4.程式片段:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim myConn As MySqlConnection
Dim myDataAdapter As MySqlDataAdapter
Dim myDataSet As Data.DataSet
myConn = New MySqlConnection("Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;")
myConn.Open()
Dim strSql = "select*from 資料表名稱"
myDataAdapter = New MySqlDataAdapter(strSql, myConn)
myDataSet = New Data.DataSet()
myDataAdapter.Fill(myDataSet, "資料表名稱")
myGrid.DataSource = myDataSet.Tables("資料表名稱").DefaultView
myGrid.DataBind()
myConn.Close()
End Sub
留言列表