如何将aspx.vb文件中的记录插入到mdb数据代码中

时间:2013-08-15 21:39:11

标签: asp.net webforms

嘿,晚上有好人可以告诉我我的代码有什么问题吗?我收到的是“操作无法更新”错误,如下面的错误

 Server Error in '/' Application.
 Operation must use an updateable query.

 Description: An unhandled exception occurred during the execution of the current 
  webrequest. Please review the stack trace for more information about the error 
  and where it originated in the code. 

  Exception Details: System.Data.OleDb.OleDbException: Operation must use an
 updateable query.

  Source Error: 


      Line 20:          cmd.Parameters.AddWithValue("paswords", TextBox4.Text)
      Line 21:          conn.Open()
      Line 22:          cmd.ExecuteNonQuery()
      Line 23:          End Using
      Line 24:          End Using

我的代码是

    Imports System.Web.Security
    Imports System.Data
    Imports System.Data.OleDb
    Imports System.Configuration
    Imports System.Web.Configuration

    Partial Class Registration
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|onlineregistration.mdb"
        Dim SqlString As String = "Insert Into registration (firtsname, telephone, email, paswords) Values (?,?,?,?)"
        Using conn As New OleDbConnection(ConnString)
            Using cmd As New OleDbCommand(SqlString, conn)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.AddWithValue("firtsname", TextBox1.Text)
                cmd.Parameters.AddWithValue("telephone", TextBox2.Text)
                cmd.Parameters.AddWithValue("email", TextBox3.Text)
                cmd.Parameters.AddWithValue("paswords", TextBox4.Text)
                conn.Open()
                cmd.ExecuteScalar()
            End Using
        End Using
    End Sub

End Class

2 个答案:

答案 0 :(得分:0)

当mdb文件是只写时,或者对文件没有写权限时,通常会抛出该错误“必须使用可更新的查询”。

答案 1 :(得分:0)

确保您的ASP.NET帐户(在大多数情况下为NETWORK SERVICE)具有对其存储的MDB文件/文件夹的读/写访问权限。有时也需要IUSR_MachineName帐户

您也可以尝试将Mode=ReadWrite;添加到连接字符串中。