【问题标题】:oRecordset in ASP.NET mySQLoASP.NET mySQL 中的记录集
【发布时间】:2010-04-09 22:13:21
【问题描述】:

我有这个连接到我的服务器的 mySQL 代码。它连接得很好:

 Dim MyConString As String = "DRIVER={MySQL ODBC 3.51 Driver};" & _
 "SERVER=example.com;" & _
 "DATABASE=xxx;" & _
 "UID=xxx;" & _
 "PASSWORD=xxx;" & _
 "OPTION=3;"

 Dim conn As OdbcConnection = New OdbcConnection(MyConString)
 conn.Open()

 Dim MyCommand As New OdbcCommand
 MyCommand.Connection = conn
 MyCommand.CommandText = "select * from userinfo WHERE emailAddress = '" & theUN & "'""
 MyCommand.ExecuteNonQuery()
 conn.Close()

但是,我有一个旧的 Classic ASP 页面,它使用“oRecordset”从 mySQL 服务器获取数据:

 Set oConnection = Server.CreateObject("ADODB.Connection")
 Set oRecordset = Server.CreateObject("ADODB.Recordset")

 oConnection.Open "DRIVER={MySQL ODBC 3.51 Driver}; SERVER=example.com; PORT=3306; DATABASE=xxx; USER=xxx; PASSWORD=xxx; OPTION=3;"
 sqltemp = "select * from userinfo WHERE emailAddress = '" & theUN & "'"
 oRecordset.Open sqltemp, oConnection,3,3

我可以按如下方式使用 oRecordset:

 if oRecordset.EOF then....

 strValue = oRecordset("Table_Name").value

 oRecordset("Table_Name").value = "New Value"
 oRecordset.update

等等……

但是,就我的一生而言,我找不到任何与我的经典 ASP 页面相似的 .net 代码!!!!!

任何帮助都会很棒! :o)

大卫

【问题讨论】:

    标签: asp.net mysql vb.net


    【解决方案1】:

    这是你必须做的:

    您应该使用 MyCommand.ExecuteQuery 并将其分配给 DataReader,而不是 MyCommand.ExecuteNonQuery。

    查看此示例:

    Dim myConnection As SqlConnection
    Dim myCommand As SqlCommand
    Dim dr As New SqlDataReader()
    'declaring the objects
    
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)_
    Handles MyBase.Load
    myConnection = New SqlConnection("server=localhost;uid=sa;pwd=;database=pubs")
    'establishing connection. you need to provide password for sql server
    Try
    myConnection.Open()
    'opening the connection
    myCommand = New SqlCommand("Select * from discounts", myConnection)
    'executing the command and assigning it to connection
    dr = myCommand.ExecuteReader()
    While dr.Read()
    'reading from the datareader
    MessageBox.Show("discounttype" & dr(0).ToString())
    MessageBox.Show("stor_id" & dr(1).ToString())
    MessageBox.Show("lowqty" & dr(2).ToString())
    MessageBox.Show("highqty" & dr(3).ToString())
    MessageBox.Show("discount" & dr(4).ToString())
    'displaying the data from the table
    End While
    dr.Close()
    myConnection.Close()
    Catch e As Exception
    End Try
    

    HTH

    【讨论】:

    • 太棒了,HTH。我做的和你的例子有点不同,但它有帮助!感谢您的帮助。
    • Ugg.. Raja,您知道如何添加新记录吗? rst.addnew 似乎不起作用?
    • 您必须使用 ExecuteNonQuery 来插入记录。看看这篇文章....startvbdotnet.com/ado/sqlserver1.aspx
    • 所以添加新记录的唯一方法是使用查询“插入..”???没有addnew命令???
    【解决方案2】:
     Dim conn As OdbcConnection = New OdbcConnection("DRIVER={MySQL ODBC 3.51 Driver}; SERVER=xxx.com; DATABASE=xxx; UID=xxx; PASSWORD=xxx; OPTION=3;")
        conn.Open()
    
        Dim MyCommand As New OdbcCommand
        MyCommand.Connection = conn
        MyCommand.CommandText = "SELECT * FROM userinfo"
        Dim rst = MyCommand.ExecuteReader()
    
        While rst.Read()
            response.write(rst("userID").ToString())        
        End While
        conn.Close()
    

    【讨论】:

      【解决方案3】:
      Dim email As String = "anyone@anywhere.com"
      Dim stringValue As String
      
      Using conn As OdbcConnection = New OdbcConnection(MyConString)
          conn.Open()
          Dim sql = "Select ... From userInfo Where emailAddress = @Email"
          Using cmd As OdbcCommand = New OdbcCommand(sql, conn)
              cmd.Parameters.AddWithValue("@Email", email)
              Dim reader As OdbcDataReader = cmd.ExecuteReader()
              While reader.Read()
                  stringValue = reader.GetString(0)
              End While
          End Using
          conn.Close()
      End Using
      
      'To do an Update
      Using conn As OdbcConnection = New OdbcConnection(MyConString)
          conn.Open()
          Dim sql As String = "Update userInfo Set Column = @Value Where PK = @PK"
          Using cmd As OdbcCommand = New OdbcCommand(sql, conn)
              cmd.Parameters.AddWithValue("@Email", email)
              cmd.ExecuteNonQuery()
          End Using
      End Using
      
      'To do an Insert
      Using conn As OdbcConnection = New OdbcConnection(MyConString)
          conn.Open()
          Dim sql As String = "Insert userInfo(Col1,Col2,...) Values(@Value1,@Value2...)"
          Using cmd As OdbcCommand = New OdbcCommand(sql, conn)
              cmd.Parameters.AddWithValue("@Col1", value1)
              cmd.Parameters.AddWithValue("@Col2", value2)
              ...
              cmd.ExecuteNonQuery()
          End Using
      End Using
      

      首先,即使在 ASP Classic 中,将值直接连接到 SQL 语句中也是一种绝对可怕的方法。这就是 SQL 注入漏洞发生的方式。您应该始终清理连接到 SQL 语句中的值。在 .NET 中,您可以使用参数化查询,将查询中的值替换为以 @ 符号开头的变量。然后,您将参数添加到命令对象并以这种方式设置您的值。 Command 对象将为您清理值。

      添加 您在评论中提到您的 ASP Classic 代码更短。事实上,.NET 代码更短,因为发生了许多您看不到的事情,也没有在您的 ASP Classic 代码中实现。我已经提到了一个对输入进行消毒的方法。另一个是记录。开箱即用,如果抛出异常,它将使用调用堆栈将其记录在事件日志中。甚至在 ASP Classic 中获得调用堆栈也是一件苦差事,更不用说任何体面的日志记录了。您需要设置 On Error Resume Next 并在每行之后检查 err.number 0。另外,如果没有 On Error Resume Next,如果抛出错误,你无法保证连接会被关闭。它应该关闭,但唯一确定的方法是使用 On Error Resume Next 并尝试关闭它。

      一般情况下,我将所有数据访问代码封装到一组方法中,这样我就可以简单地传递 SQL 语句和参数值,并确保每次都能正确调用它。 (这也适用于 ASP Classic)。

      【讨论】:

      • 嗯,谢谢你的例子,但这似乎有很多额外的代码与经典的 ASP 代码相匹配。您将如何进行 rst.addnew?
      • @StealthRT - 修改了我的答案。
      猜你喜欢
      • 1970-01-01
      • 2013-07-06
      • 1970-01-01
      • 2014-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多