从datagridview插入的数据在SQL Server中未更新 [英] Data inserted from datagridview not updated in SQL Server
问题描述
我仍在适应vb.net环境,所以我想提一个问题.
I'm still adjusting to the vb.net environment so I want to ask a question..
我的代码在这里:
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim connstr As String = "server=supertelco\sqlexpress; database=testdb; user= sa; password=sa;"
cmdconn = New SqlConnection
cmd = New SqlCommand
cmdconn.ConnectionString = connstr 'sqlstr
cmd.Connection = cmdconn
cmdconn.Open()
Dim period, VOUCH_AMT, INDIVIDUAL_AMT, check_no, D_MAILED, DIR_NO, who_updated As String
For i As Integer = 0 To Me.DataGridView2.Rows.Count - 1
With Me.DataGridView2.Rows(i)
If IsDBNull(.Cells(0).Value()) OrElse .Cells(0).Value() Is Nothing OrElse .Cells(0).Value().ToString().Trim() = "" Then
period = ""
Else
period = .Cells(0).Value()
End If
If IsDBNull(.Cells(1).Value()) OrElse .Cells(1).Value() Is Nothing OrElse .Cells(1).Value().ToString().Trim() = "" Then
VOUCH_AMT = "0"
Else
VOUCH_AMT = .Cells(1).Value()
End If
If IsDBNull(.Cells(2).Value()) OrElse .Cells(2).Value() Is Nothing OrElse .Cells(2).Value().ToString().Trim() = "" Then
INDIVIDUAL_AMT = "0"
Else
INDIVIDUAL_AMT = .Cells(2).Value()
End If
If IsDBNull(.Cells(3).Value()) OrElse .Cells(3).Value() Is Nothing OrElse .Cells(3).Value().ToString().Trim() = "" Then
check_no = ""
Else
check_no = .Cells(3).Value()
End If
If IsDBNull(.Cells(4).Value()) OrElse .Cells(4).Value() Is Nothing OrElse .Cells(4).Value().ToString().Trim() = "" Then
D_MAILED = ""
Else
D_MAILED = .Cells(4).Value()
End If
If IsDBNull(.Cells(5).Value()) OrElse .Cells(5).Value() Is Nothing OrElse .Cells(5).Value().ToString().Trim() = "" Then
DIR_NO = ""
Else
DIR_NO = .Cells(5).Value()
End If
If IsDBNull(.Cells(6).Value()) OrElse .Cells(6).Value() Is Nothing OrElse .Cells(6).Value().ToString().Trim() = "" Then
who_updated = ""
Else
who_updated = .Cells(6).Value()
End If
End With
cmd.CommandText = "insert into tobee.EBD_BILLHISTORY(period, vouch_amt, individual_amt, check_no, d_mailed, dir_no, who_updated)values" & _
"('" & period.Replace("'", "''") & "'," & VOUCH_AMT & "," & INDIVIDUAL_AMT & ",'" & check_no.Replace("'", "''") & "','" & D_MAILED.Replace("'", "''") & "', '" & DIR_NO.Replace("'", "''") & "','" & who_updated.Replace("'", "''") & "')"
cmd.ExecuteNonQuery()
MsgBox("Saved")
Next
cmdconn.Close()
End Sub
是的,除了SQL Server中的更新之外,所有代码都可以正常工作.我想知道为什么单击保存按钮后在dgv中所做的更改未反映在数据库中?有人可以帮我吗?我想念什么吗?
so yeah, all code works fine except the updates in SQL Server. I wonder why the changes I made in dgv was not being reflected in the database after clicking the save button? Could someone help me about this? Am I missing something?
推荐答案
我建议在调用ExecuteNonQuery到剪贴板之前,将值cmd.CommandText复制到剪贴板,然后在Sql Server Management Studio中执行它……看看会发生什么情况.(错误消息)...除此之外,您还应该考虑使用SqlParameters ...
I would suggest to copy the value cmd.CommandText has before you call ExecuteNonQuery to the clipboard and execute it in Sql Server Management Studio ... and see what happens (error messages) ... apart from this you should think about using SqlParameters ...
这篇关于从datagridview插入的数据在SQL Server中未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!