2013-04-04 60 views
-3

我正在asp.net中開發一個小型web應用程序,並使用mysql作爲後端。所以爲了做mysql數據庫的數據庫連接,我已經下載了'MySql.Data.dll'並添加爲項目中的參考。所以我的問題是,是否有任何更改,我必須在'web.config'?asp.net中的mysql數據庫連接

回答

2

您可以使用MySql Connector。 MySqlCOnnector.和Demo。 MYSQL Connection

MySql.Data.MySqlClient.MySqlConnection mycon = 
    new MySqlConnection("YourConnectionStringHere); 
0

使用下列進口的MySQL

使用MySql.Data.MySqlClient;

1,創建新網站在Visual Studio中,並將其保存

2.Now打開Default.aspx的形式和拖動一些標籤,文本框和按鈕。

<asp: Label ID="Label1" runat="server" Text="Name"></asp:Label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br/> <br/><asp:Label ID="Label2" runat="server" Text="Address"></asp:Label>&nbsp;&nbsp;&nbsp;<asp:TextBox ID="TextBox2" runat="server"></asp: Textbox> <br /> <br /><asp:Label ID="Label3" runat="server" Text="Age"></asp:Label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:TextBox ID="TextBox3" runat="server"></asp: Textbox> <br /> <br /> <br /> <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /> <br /> <br /> <br /> <br /> 

4.Now,用於建立連接,你需要這個

public partial class _Default : System.Web.UI.Page 

{ 
MySqlConnection con; 
MySqlCommand cmd; 
string str; 
} 

5.Now在Page_Load事件。

protected void Page_Load(object sender, EventArgs e) 
{ 
con = new MySqlConnection("Data Source=localhost;Database=YourDatabase Name;User ID=root;Password=YourPasssword"); 
con.Open(); 
Response.Write("connect"); 
} 

6.Now寫button_click事件的代碼

protected void Button1_Click(object sender, EventArgs e) 
{ 
str = "insert into YourTablename values ('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "')"; 
cmd = new MySqlCommand(str, con); 
cmd.ExecuteNonQuery(); 
} 

你可以找到這個樂於助人的太....這個鏈接 http://www.c-sharpcorner.com/UploadFile/brij_mcn/mysql-database-connectivity-with-Asp-Net/

0
<% 
'declare the variables 
Dim Connection 
Dim ConnectionString 
Dim Recordset 
Dim SQL 

'declare the SQL statement that will query the database 
SQL = "SELECT * FROM TABLE_NAME" 

'define the connection string, specify database driver 
ConnString = "DRIVER={MySQL ODBC 3.51 Driver}; SERVER=localhost; DATABASE=Your_Mysql_DB; " &_ 
"UID=mysql_username;PASSWORD=mysql_password; OPTION=3" 

'create an instance of the ADO connection and recordset objects 
Set Connection = Server.CreateObject("ADODB.Connection") 
Set Recordset = Server.CreateObject("ADODB.Recordset") 

'Open the connection to the database 
Connection.Open ConnString 

'Open the recordset object executing the SQL statement and return records 
Recordset.Open SQL,Connection 

'first of all determine whether there are any records 
If Recordset.EOF Then 
Response.Write("No records returned.") 
Else 
'if there are records then loop through the fields 
Do While NOT Recordset.Eof 
Response.write Recordset("FIRST_FIELD_NAME") 
Response.write Recordset("SECOND_FIELD_NAME") 
Response.write Recordset("THIRD_FIELD_NAME") 
Response.write "<br>"  
Recordset.MoveNext  
Loop 
End If 

'close the connection and recordset objects freeing up resources 
Recordset.Close 
Set Recordset=nothing 
Connection.Close 
Set Connection=nothing 
%> 

http://webcheatsheet.com/ASP/database_connection_to_MySQL.php

+0

如果我們用.NET編寫,我們將利用內置的類。它們遠遠超過ADODB優化,而且這些對象是非託管資源,難以清理。 – 2013-04-04 08:57:55

1

如果您想連接到您合作的數據庫ULD做這樣的事情:

using (MySqlConnection c = new MySqlConnection("connection string here")) 
{ 
    c.Open(); 

    // and now let's select some data 
    MySqlCommand cmd = new MySqlCommand("SELECT * FROM SomeTable", c); 

    MySqlDataReader rdr = cmd.ExecuteReader(); 
    while (rdr.Read()) 
    { 
     // do something with the fields here 
    } 
} 

,然後如果你想執行INSERTUPDATE,或DELETE聲明做這樣的事情:

using (MySqlConnection c = new MySqlConnection("connection string here")) 
{ 
    c.Open(); 

    // and now let's select some data 
    MySqlCommand cmd = new MySqlCommand("UPDATE SomeTable SET Field1 = 'some value' WHERE some where clause", c); 

    cmd.ExecuteNonQuery(); 
} 

,並請leverage the documentation,讓你的休息因爲我不知道你還想做什麼。從那裏你可以到達MySqlCommand和其他課程。

最後,你需要在參數化查詢閱讀,因爲這種說法例如,UPDATE SomeTable SET Field1 = 'some value' WHERE some where clause,真的應該像UPDATE SomeTable SET Field1 = @Field1 WHERE some where clause然後參數設置的命令是這樣的:

cmd.AddWithValue("@Field1", "some value"); 

,如果有任何WHERE子句中的靜態值同樣適用。

0

只需按照簡單說明here

您必須下載數據庫連接庫。只需按照鏈接中的步驟操作即可。

享受。