1

我使用vb.net的功能。我是查询sql到datagridview并从datagridview插入数据到Databse按功能。如何使用函数VB.NET插入数据库?

但功能错误:在此上下文中不允许使用名称'EXHBK13004'。这里只允许常量,表达式或变量。列名不被允许。

我想用插入到数据库的函数。

表CLOTHER

Name  Type 
No (PK) int 
Code  nvarchar(12) 
RClother int 
CIDetail int 
PO  nvarchar(50) 

代码(按钮保存)

Private Sub btSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSave.Click 

    For i As Integer = 0 To DgvItem.Rows.Count - 1 
     sendPDTStatus = FInsertClother(CStr(DgvItem.Rows(i).Cells(0).Value), CInt(DgvItem.Rows(i).Cells(1).Value), CInt(DgvItem.Rows(i).Cells(2).Value), _ 
     DgvItem.Rows(i).Cells(3).Value) 
    Next 

End Sub 

代码功能

Public Function FInsertClother(ByVal Code As String, ByVal RClother As Integer, ByVal CIDetail As Integer, ByVal PO As String)  
      Dim Tr As SqlTransaction 
      Dim sqlCom As New SqlCommand 

      Dim sqlInsert As String 
      Dim ReturnValue As Integer 

      Tr = Conn.BeginTransaction 
      sqlCom.Connection = Conn 

      sqlInsert = "INSERT INTO Clother " 
      sqlInsert &= "(Code,RClother,CIDetail,PO) " 
      sqlInsert &= "VALUES(" & Code & "," & RClother & "," & CIDetail & "," & PO & ")" 

      sqlCom.Transaction = Tr 
      sqlCom.CommandText = sqlInsert 
      sqlCom.CommandType = CommandType.Text 

      ReturnValue = sqlCom.ExecuteScalar << Line Error 
      If ReturnValue = 0 Then 
       Tr.Commit() 
      Else 
       Tr.Rollback() 
      End If 
    Return ReturnValue  
End Function 

我尝试调试这个结果

Name     Value 
sqlCom.CommandText "INSERT INTO Clother (Code,RClother,CIDetail,PO) VALUES(050030543003,5022,30543,EXHBK13004/3)" 

sqlInsert   "INSERT INTO Clother (Code,RClother,CIDetail,PO) VALUES(050030543003,5022,30543,EXHBK13004/3)" 

只有场“PO”不插入到数据库。

谢谢你的时间。 :))

回答

1

关闭连接。您需要将字符串值放在引号中。

sqlInsert &= "VALUES('" & Code & "'," & RClother & "," & CIDetail & ",'" & PO & "')" 

也就是说,你不应该使用连接来建立一个查询字符串。这使您的查询受到SQL注入攻击。相反,你应该使用参数化查询。 (正如史蒂夫在他的回答中所表明的)。

2

首先,我会删除字符串连接,并使用参数化查询,以避免解析问题和Sql注入(在您的代码中,您已经传递了两个字符串而不使用引号,这肯定会导致插入失败,因为字符串字段需要一个引号分隔符)

然后我也删除事务,因为现在循环执行并确认每行的单个命令。

此外,您似乎有一个全局连接对象,这是一个不好的做法,您应该打开连接并尽快关闭它,而不会在应用程序的整个生命周期中保持打开状态。

Public Function FInsertClother(ByVal Code As String, ByVal RClother As Integer, ByVal CIDetail As Integer, ByVal PO As String)  

    Dim sqlInsert As String 
    Dim ReturnValue As Integer 

    sqlInsert = "INSERT INTO Clother " & _ 
       "(Code,RClother,CIDetail,PO) " & _ 
       "VALUES(@code, @clot, @id, @po)" 

    Using sqlCom = new SqlCommand(sqlInsert, conn) 
     sqlCom.Connection = Conn 
     sqlCom.Parameters.AddWithValue("@code",Code) 
     sqlCom.Parameters.AddWithValue("@clot", RClother) 
     sqlCom.Parameters.AddWithValue("@id",CIDetail) 
     sqlCom.Parameters.AddWithValue("@po",PO) 
     ReturnValue = sqlCom.ExecuteNonQuery 
     Return ReturnValue  
    End Using 
End Function 

一个非常有用的增强功能是打开按钮单击连接并将其传递给此函数。因此,当您完成循环遍历行时,您可以通过Using Statement

+1

不错的快速编码。 – jfrankcarr

相关问题