将一个datagridview值添加到sql表中 [英] add a datagridview values to sql table

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

问题描述

您好,

我有一个连接到SQL表的数据网格,如何以编程方式使用按钮可以将特定的单元格值插入到另一个SQL数据库表中?

这里是vb net code给出错误column0已声明



Hello,
I have a datagrid connected to an SQL table, how to programmatically with a button can insert specific cell values to another SQL database table?
here is vb net code gives an error "column0 has been declared"

Dim ConnString As String = "Data Source=SUPERVISOR-PC;Initial Catalog=library;Integrated Security=True"
      Dim i As New Integer

      For i = 0 To DataGridView1.RowCount - 1

          Dim sql As String = "" & "INSERT INTO [atzenta].dbo.phones (" & "name, mobile, telephone" & ") VALUES (" & "@Column" & (i) & ", @Column" & (i) & ", @Column" & (i) & " )"

          Using cn As New SqlConnection(ConnString), cmd As New SqlCommand(sql, cn)

              cmd.Parameters.Add("@column" & (i), SqlDbType.VarChar, 50).Value = DataGridView1.Rows(i).Cells(1).Value
              cmd.Parameters.Add("@column" & (i), SqlDbType.VarChar, 50).Value = DataGridView1.Rows(i).Cells(4).Value
              cmd.Parameters.Add("@column" & (i), SqlDbType.VarChar, 50).Value = DataGridView1.Rows(i).Cells(5).Value

              cn.Open()
              cmd.ExecuteNonQuery()
              cn.Close()
          End Using


      Next

  End Sub





将SQL字符串更改为此并获取cmd.executenonqueryincorect syntax Near Trans



changed SQL string to this and get cmd.executenonquery "incorect syntax Near Trans"

Dim sql1 As String = "" & "INSERT INTO [atzenta].dbo.phones (" & "name, mobile, telephone" & ") VALUES (" & DataGridView1.Rows(i).Cells(1).Value & "," & DataGridView1.Rows(i).Cells(4).Value & ", " & DataGridView1.Rows(i).Cells(5).Value & ")"



并删除了添加参数部分


and deleted the add parameter section

Dim ConnString As String = "Data Source=SUPERVISOR-PC;Initial Catalog=library;Integrated Security=True"
       Dim i As New Integer

       For i = 0 To DataGridView1.RowCount - 1

           Dim sql1 As String = "" & "INSERT INTO [atzenta].dbo.phones (" & "name, mobile, telephone" & ") VALUES (" & DataGridView1.Rows(i).Cells(1).Value & "," & DataGridView1.Rows(i).Cells(4).Value & ", " & DataGridView1.Rows(i).Cells(5).Value & ")"

           Using cn As New SqlConnection(ConnString), cmd As New SqlCommand(sql1, cn)
               cn.Open()
               cmd.ExecuteNonQuery()
               cn.Close()
           End Using



任何正确的SQL字符串语法?


Any correct SQL string syntax for this?

推荐答案

所有这些参数声明都在同一个循环中。



让我告诉你第一个循环中的值:



All of these parameter declarations are within the same loop.

Let me show you the values in the first loop:

'First loop so i = 0
 
'This is just a string at this stage
Dim sql As String = "" & "INSERT INTO [atzenta].dbo.phones (" & "name, mobile, telephone" & ") VALUES (" & "@Column0, @Column0, @Column0 )"
 
Using cn As New SqlConnection(ConnString), cmd As New SqlCommand(sql, cn)
 
'Paremeter @column0  added
cmd.Parameters.Add("@column0", SqlDbType.VarChar, 50).Value = DataGridView1.Rows(i).Cells(1).Value
'Paremeter @column0 added - oh wait!  it has already been added - ERROR!
cmd.Parameters.Add("@column0", SqlDbType.VarChar, 50).Value = DataGridView1.Rows(i).Cells(4).Value
 cmd.Parameters.Add("@column0", SqlDbType.VarChar, 50).Value = DataGridView1.Rows(i).Cells(5).Value
 
cn.Open()
cmd.ExecuteNonQuery()
cn.Close()
End Using







我希望有助于澄清问题。



如果您有任何其他问题,请告诉我^ _ ^




I hope that helps clarify the issue.

Let me know if you have any further problems ^_^


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

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