【问题标题】:mysql database connectivity in asp.net [closed]asp.net中的mysql数据库连接[关闭]
【发布时间】:2013-03-26 06:00:24
【问题描述】:

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

【问题讨论】:

    标签: c# asp.net mysql


    【解决方案1】:

    只需遵循简单的说明here

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

    享受吧。

    【讨论】:

      【解决方案2】:

      如果你想连接到数据库,你可以这样做:

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

      【讨论】:

        【解决方案3】:
        <% 
        '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

        【讨论】:

        • 如果我们使用 .NET 编写,让我们利用内置类。它们的优化远远超过ADODB,而且这些对象是更难清理的非托管资源。
        【解决方案4】:

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

        【讨论】:

          【解决方案5】:

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

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2010-09-24
            • 2020-09-16
            • 1970-01-01
            • 2015-01-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多