且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在表格中添加行

更新时间:2023-12-04 08:50:10

您所有insertCmd.Parameters.Add语句中的参数名称都必须与名称匹配.例如,在您的SQL语句中,您的值由@EeID@DdID等指定.在参数中,您省略了"@"字符.这些参数名称不匹配,因此会出现此错误.更改参数语句的名称"以匹配您在SQL语句中使用的名称.
Your parameter names in all of your insertCmd.Parameters.Add statements must match the names. For example, in you''re SQL statement, your values are specified by @EeID, @DdID, and so on... In your Parameters, you''re leaving out the "@" character. Those parameter names don''t match, so you''ll get this error. Change the Parameter statements "names" to match what you have in the SQL statements.


请参阅下面设置了参数的代码修改.

See modification of your code below where the parameters are set.

' Ddataset is defined at start of Sub Button1_Click()

       Dim strKDisctBridge As String = "SELECT * FROM KDisctBridge WHERE  EeID=? "
       Dim tblKDisctBridge As New SqlCommand(strKDisctBridge, Ddataset)
       Dim sqlInsert As String = "INSERT INTO KDistrictBridge " & _
         "(EeID,DdID,District,CcID,DisctSet) VALUES " & _
         "(@EeID,@DdID,@District,@CcID,@DisctSet)"
Try
             '  make the data adapter
       Dim da As New SqlDataAdapter
          da.SelectCommand = New SqlCommand(strKDisctBridge, Ddataset)
             ' make and fill the KDisctBridge dataset
       Dim ds As New DataSet
          da.Fill(ds, "KDisctBridge")
             ' get the datatable out of the Ddataset list of tables
       Dim dt As DataTable = ds.Tables("KDisctBridge")
             ' add a row to the table
       Dim newrow As DataRow = dt.NewRow
          newrow("EeID") = 1
          newrow("DdID") = 2
          newrow("District") = "Able"
          newrow("CcID") = 3
          newrow("DisctSet") = "Baker"
          dt.Rows.Add(newrow)
             ' insert the new row
             ' create the command
       Dim insertCmd As New SqlCommand(sqlInsert, Ddataset)

          insertCmd.Parameters.Add("@EeID",SqlDbType.Int).Value=1
          insertCmd.Parameters.Add("@DdID",SqlDbType.Int).Value=2
          insertCmd.Parameters.Add("@CcID",SqlDbType.Int).Value=3

          insertCmd.Parameters.Add("@District",SqlDbType.Char).Value="Able"
          insertCmd.Parameters.Add("@DisctSet",SqlDbType.Char).Value="Baker"

          da.InsertCommand = insertCmd
          da.Update(ds, "KDisctBridge")
 Catch ex As SqlException
          MsgBox("error at catch" & ex.ToString())
          Console.WriteLine("Error: " & ex.ToString())
 Finally
          Ddataset.Close()
 End Try
 End Using
 End Sub