-4

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

4

5 回答 5

2

您可以使用 MySql 连接器。MySql连接器。和演示。MYSQL 连接

MySql.Data.MySqlClient.MySqlConnection mycon = 
  new MySqlConnection("YourConnectionStringHere);
于 2013-04-04T08:53:11.433 回答
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
    }
}

然后,如果您想执行INSERT,UPDATEDELETE语句,请执行以下操作:

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();
}

利用文档来帮助您完成剩下的工作,因为我不知道您还想做什么。从那里你可以去那里的链接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子句中有任何静态值,则同样适用。

于 2013-04-04T08:55:55.893 回答
0

只需按照此处的简单说明进行操作。

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

享受。

于 2013-04-04T09:15:22.057 回答
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();
}

你也可以找到这个 Helpfull ......通过这个链接 http://www.c-sharpcorner.com/UploadFile/brij_mcn/mysql-database-connectivity-with-Asp-Net/

于 2013-04-04T08:53:50.813 回答
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

于 2013-04-04T08:55:32.417 回答