asp.net中的mysql数据库连接

时间:2013-04-04 08:49:22

标签: c# asp.net mysql

我正在asp.net中开发一个小型Web应用程序,并使用mysql作为后端。所以为了做mysql数据库的数据库连接,我已经下载了'MySql.Data.dll'并在项目中添加了作为参考。所以我的问题是,我必须在'web.config'中做出任何改变吗?

5 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

如果您想连接数据库,可以执行以下操作:

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
    }
}

然后如果你想执行INSERTUPDATEDELETE语句,请执行以下操作:

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子句中有任何静态值,则同样适用。

答案 2 :(得分:0)

对MySQl使用以下导入

使用MySql.Data.MySqlClient;

1.在Visual Studio中创建新网站并保存

2.现在打开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.现在要创建连接,您需要这个

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

{
MySqlConnection con;
MySqlCommand cmd;
string str;
}

5.现在在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.现在在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/

答案 3 :(得分: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

答案 4 :(得分:0)

只需遵循简单的说明here

您必须下载用于数据库连接的库。只需按照链接中的步骤操作即可。

享受。