来自datagridview的SQL INSERT [英] SQL INSERT from datagridview
本文介绍了来自datagridview的SQL INSERT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试插入sql server。但是除了datagridview列(1)中的值之外,所有数据都会被插入。请通知错误的地方。请帮我。列(1)中的值总是像发票金额一样的数值。
Button1点击事件
******** ************************************************** ********
Dim conn 正如 字符串 =( 数据源= BASHEER-PC;初始目录=同志;用户ID = sa;密码= QDCqdc123)
Dim Cn 作为 新 SqlConnection(conn)
Dim mycommand As 新 SqlCommand
mycommand.CommandType = CommandType.StoredProcedure
mycommand.CommandText = Invapp
Dim i As 整数
Convert.ToInt32(DataGridView3.Rows(i).Cells( 1 )。Value)
对于 i = 0 DataGridView3.RowCount - 1
mycommand.Parameters.Add( @ Voucher_Type, SqlDbType.NChar).Value = RV
mycommand.Parameters.Add( @ voucher_no,SqlDbType.Int).Value = Txtrvno.Text
mycommand.Parameters.Add ( @ Account_number,SqlDbType.VarChar).Value = Txtcraccountno.Text
mycommand。参数.Add( @ Account_name,SqlDbType.VarChar).Value = Txtcraccountname.Text
mycommand.Parameters.Add( @ Debit_Amount,SqlDbType.Money).Value = 0
mycommand.Parameters.Add( @ Credit_Amount,SqlDbType.Money).Value = 0
mycommand.Parameters.Add( @ Voucher_date,SqlDbType.SmallDateTime).Value = Txtdate.Text
mycommand.Parameters.Add( @ Bal_Amount,SqlDbType.NVarChar).Value = DataGridView3.Rows(i).Cells( 1 )。价值
mycommand.Connection = Cn
尝试
Cn.Open()
mycommand.ExecuteNonQuery()
MsgBox( 成功发布,MsgBoxStyle.Information)
Cn.Close()
Catch ex As 异常
' 抛出前
最后
Cn.Dispose()
' 结束捕获
结束 尝试
下一步
解决方案
对于每个rw As DataGridViewRow 在 DataGridView3.Rows
mycommand.Parameters。添加( @ Bal_Amount,SqlDbType。 NVarChar )。值= rw.Cells( 1 )。值
下一个
或
对于每个rw As DataGridViewRow In DataGridView3 。$ $ $ $ $ mycommand.Parameters。添加( @Bal_Amount,SqlDbType。 NVarChar )。Value = rw.Cells( < span class =code-string> Amt)。价值
下一步
你试试这段代码
试试这个
mycommand.Parameters.Add( @ Bal_Amount,SqlDbType.NVarChar)。 Value = DataGridView3.Rows(i).Cells( 1 )。Value.ToString()
和希望你知道它是否是你的datagridview的第一列然后你必须使用
DataGridView3.Rows(i).Cells( 0 )。Value.ToString() // 索引以0开头
I am trying to insert to sql server. But all datas are inserted except the values in the datagridview column(1). Please notify where I did mistake. Please help me. The values in columns(1) always a numerical value like invoice amount.
Button1 click event
******************************************************************
Dim conn As String = ("Data Source=BASHEER-PC;Initial Catalog=comrade;User ID=sa;Password=QDCqdc123")
Dim Cn As New SqlConnection(conn)
Dim mycommand As New SqlCommand
mycommand.CommandType = CommandType.StoredProcedure
mycommand.CommandText = "Invapp"
Dim i As Integer
Convert.ToInt32(DataGridView3.Rows(i).Cells(1).Value)
For i = 0 To DataGridView3.RowCount - 1
mycommand.Parameters.Add("@Voucher_Type", SqlDbType.NChar).Value = "RV"
mycommand.Parameters.Add("@voucher_no", SqlDbType.Int).Value = Txtrvno.Text
mycommand.Parameters.Add("@Account_number", SqlDbType.VarChar).Value = Txtcraccountno.Text
mycommand.Parameters.Add("@Account_name", SqlDbType.VarChar).Value = Txtcraccountname.Text
mycommand.Parameters.Add("@Debit_Amount", SqlDbType.Money).Value = 0
mycommand.Parameters.Add("@Credit_Amount", SqlDbType.Money).Value = 0
mycommand.Parameters.Add("@Voucher_date", SqlDbType.SmallDateTime).Value = Txtdate.Text
mycommand.Parameters.Add("@Bal_Amount", SqlDbType.NVarChar).Value = DataGridView3.Rows(i).Cells(1).Value
mycommand.Connection = Cn
Try
Cn.Open()
mycommand.ExecuteNonQuery()
MsgBox("Posted Sucessfully", MsgBoxStyle.Information)
Cn.Close()
Catch ex As Exception
' Throw ex
Finally
Cn.Dispose()
' end catch
End Try
Next
解决方案
For Each rw As DataGridViewRow In DataGridView3.Rows mycommand.Parameters.Add("@Bal_Amount", SqlDbType.NVarChar).Value = rw.Cells(1).Value Next OR For Each rw As DataGridViewRow In DataGridView3.Rows mycommand.Parameters.Add("@Bal_Amount", SqlDbType.NVarChar).Value = rw.Cells("Amt").Value Next
you try this code
try this
mycommand.Parameters.Add("@Bal_Amount", SqlDbType.NVarChar).Value = DataGridView3.Rows(i).Cells(1).Value.ToString()
and hope you know if it is the first column of your datagridview then you had to use
DataGridView3.Rows(i).Cells(0).Value.ToString()//as index starts with 0
这篇关于来自datagridview的SQL INSERT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文