10月 27, 2006

.net 2.0連結Mysql

最近在GOdaddy租了.net的虛擬主機,規格不錯價格也划算
我租的是沒有MSsql的版本,只能用mysql或access
godaddy提供了OleDB和ODBC的方法供.net連結mysql,不過我都試不出來orz...
到google搜尋了一下,發現多數人都用mysql專為.net寫的driver來連mysql db
方法如下
1.在專案中建立/bin資料夾
2.將MySql.Data.dll放置/bin資料夾
3.在程式中引用MySql.Data.dll(vb寫法:)
以下是code

檔案:http://joehwang.myweb.hinet.net/xuite/DonetAndMysql.zip
  
<%@Page Language="VB" CodePage="65001" %>
<%@Import Namespace = "MySql.Data.MySqlClient" %>
<script runat="server">
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
Dim strSQL As String
'建立連接
myConn = New MySqlConnection("server=mysql213.secureserver.net; user id=joehwang; password=b0410; database=joehwang;CHARSET=big5")
myConn.Open()
Dim mycmd As MySqlCommand
mycmd = New MySqlCommand("Update member Set name = '一' where id = 1", myConn)
mycmd.ExecuteNonQuery()

strSQL = "Select * FROM member;"
myDataAdapter = New MySqlDataAdapter(strSQL, myConn)
myDataSet = New Data.DataSet

myDataAdapter.Fill(myDataSet, "mytable")
GridView1.DataSource = myDataSet.Tables("mytable").DefaultView
GridView1.DataBind()
myConn.Close()

End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server" lang="zh-tw">
<title>未命名頁面</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#E3EAEB" />
<EditRowStyle BackColor="#7C6F57" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<br />
測試
</div>
</form>
</body>
</html>



沒有留言: