在表格中添加行 [英] add row to table

查看:69
本文介绍了在表格中添加行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码是用VWD 2010 Express编写的.这段代码试图将五列的一行添加到表KDistrictBridge中.它没有做到.
我收到有关声明标量变量@EeID的消息.在论坛上,我似乎应该写EeID=?. ,所以我进行了更改. (请参阅第一个昏暗声明)
现在,我收到一条消息附近语法不正确?".

有谁知道如何编写代码以将一行中的五列放在一个表中?那是我实际上正在尝试做的事情.当我能够做到这一点时,我将尝试将一个数组复制到KDisctBridge中.但是首先要注意的是.

非常感谢您的帮助.

This code is written in VWD 2010 express. This code is trying to add one row of five columns to the table KDistrictBridge. It doesn''t do it.
I got a message about declaring a scalar variable @EeID. Looking at forums, it appeared that I should write EeID=?. And so I made the change. (see first dim statement)
Now, I get a message "Incorrect syntax near ?".

Does anyone know how to write the code to put one row of five columns in a table? That what I am actually trying to do. When I can do that, then I will try to get an array copied into KDisctBridge. But first things first.

Many thanks for your help.

     ' 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(New SqlParameter("", "EeID"))
          insertCmd.Parameters.Add(New SqlParameter("", "DdID"))
          insertCmd.Parameters.Add(New SqlParameter("", "District"))
          insertCmd.Parameters.Add(New SqlParameter("", "CcID"))
          insertCmd.Parameters.Add(New SqlParameter("", "DisctSet"))
          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

推荐答案

您所有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


这篇关于在表格中添加行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆