【问题标题】:I can't insert value in my acces database我无法在我的访问数据库中插入值
【发布时间】:2011-12-16 04:59:25
【问题描述】:

这是我在 VB.NET 中的代码。

我的 try catch 说指令 INSERT INTO 中存在语法错误。我不知道我的 INSERT 发生了什么。我搜索了一个多小时的错误...我不是 VB.NET 方面的专家,我在 C# 方面做得更好,但无论如何我都需要在 VB 中这样做...

谢谢你帮助我!!

Sub InsertRecord()
Dim conClasf As OleDbConnection
Dim cmdClasf As New OleDbCommand
Dim strClasf As String
Dim strSQL As String
Dim intRowsAff As Integer

 lblErrMsg.Text = ""
 lblRecsAff.Text = ""
  strClasf = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
         "Data Source=" & _
         server.mappath("BecsEtMuseaux.mdb") & ";"
 conClasf = New OleDbConnection(strClasf)
 conClasf.Open

Randomize
    strSQL = "INSERT INTO client (" & _
        "UserName, " & _
        "Prenom, " & _
        "Nom, " & _
        "password, " & _
        "mail, " & _
        "Addresse, " & _
        "Ville, " & _
        "PostalCode, " & _
        "Province, " & _
        "Pays, " & _
        "AnimalGenre, " & _
        "NomAnimal, " & _
        "Race, " & _
") VALUES ('" & _
        Replace(txtUserName.Text, "'", "''") & _
        "', '" & _
        Replace(txtPrénom.Text, "'", "''") & _
        "', '" & _
        Replace(txtNom.Text, "'", "''") & _
        "', '" & _
        Replace(txtPass.Text, "'", "''") & _
        "', '" & _
        Replace(txtMail.Text, "'", "''") & _
        "', " & _
        Replace(txtAdresse.Text, "'", "''") & _
        "', " & _
         Replace(txtVille.Text, "'", "''") & _
        "', " & _
        Replace(txtPostal.Text, "'", "''") & _
        "', " & _
         Replace(txtProvince.Text, "'", "''") & _
        "', " & _
         Replace(txtPays.Text, "'", "''") & _
        "', " & _
         Replace(rblAnimal.Text, "'", "''") & _
        "', " & _
        Replace(txtAnimal.Text, "'", "''") & _
        "', " & _
         Replace(txtRace.Text, "'", "''")

      cmdClasf = New OleDbCommand(strSQL, conClasf)
      Try
      intRowsAff = cmdClasf.ExecuteNonQuery()
      Catch ex As Exception
      lblErrMsg.Text = ex.Message    
      End Try
      lblRecsAff.Text = intRowsAff & " record(s) inserted"
      conClasf.Close
 End Sub

【问题讨论】:

    标签: asp.net vb.net ms-access


    【解决方案1】:

    转义密码字段。为Access Engine.的保留字。

    strSQL = "INSERT INTO client (UserName,Prenom,Nom,[password],mail,
              Addresse,Ville,PostalCode,Province,Pays,AnimalGenre,NomAnimal,Race)
              VALUES 
              (@UserName,@Prenom,@Nom,@password,@mail,
                @Addresse,@Ville,@PostalCode,@Province,@Pays,
                 @AnimalGenre,@NomAnimal,@Race)"
    

    【讨论】:

      【解决方案2】:

      也许这会导致您出错。

             "Race, " & _ //Race**,** try to remove ,
      ")
      

      如果你试试这个会更好:

      你可以使用Parameters来避免sqlinjection。

        strSQL = "INSERT INTO client (UserName,Prenom,Nom,password,mail,Addresse,Ville,PostalCode,Province,Pays,AnimalGenre,NomAnimal,Race) VALUES (@UserName,@Prenom,@Nom,@password,@mail,@Addresse,@Ville,@PostalCode,@Province,@Pays,@AnimalGenre,@NomAnimal,@Race)"
      
        cmdClasf.Parameters.Add("@UserName", OleDbType.VarChar, 50).Value = txtUserName.Text
       //DO OTHER STUFF UNITL @Race
      

      另见:

      Parameter Queries in ASP.NET with MS Access

      Configuring Parameters and Parameter Data Types (ADO.NET)

      Use Parameters in your sql command

      希望对您有所帮助。

      问候

      【讨论】:

      • 这不是 SqlDbType 这是访问你确定这会起作用
      • 它的VB。尝试一下。使用 MS Access 的 ASP.NET 中的参数查询
      • 我尝试使用您的提示参数,但在指令 INSERT INTO 时仍然出现错误
      猜你喜欢
      • 2017-04-03
      • 1970-01-01
      • 2020-02-12
      • 2021-09-28
      • 1970-01-01
      • 2016-04-27
      • 2016-07-06
      • 2013-06-23
      • 2020-08-07
      相关资源
      最近更新 更多