我正在asp.net中開發一個小型web應用程序,並使用mysql作爲後端。所以爲了做mysql數據庫的數據庫連接,我已經下載了'MySql.Data.dll'並添加爲項目中的參考。所以我的問題是,是否有任何更改,我必須在'web.config'?asp.net中的mysql數據庫連接
-3
A
回答
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> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br/> <br/><asp:Label ID="Label2" runat="server" Text="Address"></asp:Label> <asp:TextBox ID="TextBox2" runat="server"></asp: Textbox> <br /> <br /><asp:Label ID="Label3" runat="server" Text="Age"></asp:Label> <asp:TextBox ID="TextBox3" runat="server"></asp: Textbox> <br /> <br /> <br /> <br />
<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
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
}
}
,然後如果你想執行INSERT
,UPDATE
,或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
相關問題
- 1. Asp.net mysql數據庫連接
- 2. MySQL數據庫和ASP.NET全局連接
- 3. 從ASP.Net連接到mySQL數據庫(vb)
- 4. Azure - ASP.NET MVC連接到mysql數據庫
- 5. 連接錯誤,同時連接到MySQL數據庫在asp.net
- 6. asp.net數據庫連接
- 7. 數據庫連接:Web.config Asp.net
- 8. asp.net數據庫連接
- 9. ASP.NET數據庫連接
- 10. ASP.NET中的數據庫連接
- 11. web.config中的oracle數據庫連接asp.net
- 12. PHP - MySQL數據庫連接
- 13. Mysql數據庫未連接
- 14. Netbeans MySql數據庫連接
- 15. 連接到MySQL數據庫
- 16. Laravel/MySQL數據庫連接
- 17. C++ MySQL數據庫連接
- 18. PHP MySQL數據庫連接
- 19. 連接到MySQL數據庫
- 20. PHP,MySQL數據庫連接
- 21. 連接到MySQL數據庫
- 22. MySQL數據庫連接vb.net
- 23. 連接到MySQL數據庫
- 24. android連接mysql數據庫
- 25. 2 mysql數據庫連接
- 26. 數據庫連接golang mysql
- 27. 連接到MySQL數據庫
- 28. 連接到MySQL數據庫
- 29. 連接到MySQL數據庫
- 30. 連接MySQL數據庫Java
如果我們用.NET編寫,我們將利用內置的類。它們遠遠超過ADODB優化,而且這些對象是非託管資源,難以清理。 – 2013-04-04 08:57:55