从datagridview到数据库的合并查询不执行sql,vb.net
从执行合并查询时遇到问题,无法更新从datagridview到数据库的合并查询不执行sql,vb.net,sql,sql-server,vb.net,datagridview,merge,Sql,Sql Server,Vb.net,Datagridview,Merge,从执行合并查询时遇到问题,无法更新DataGridView表中的值或将其插入sql server数据库表中。下面是我的代码,它没有给我任何错误或中断,但是我最近注意到它在我的数据库表dbo.schedule中创建了完整的行,其中包含所有NULL值,甚至键位置,有人能帮我吗?我不太熟悉sql中的合并查询,因此请指出我的语法存在的问题: Dim query As String = String.Empty query &= "DECLARE @TaskID nvarchar(8), @T
DataGridView
表中的值或将其插入sql server数据库表中。下面是我的代码,它没有给我任何错误或中断,但是我最近注意到它在我的数据库表dbo.schedule中创建了完整的行,其中包含所有NULL值,甚至键位置,有人能帮我吗?我不太熟悉sql中的合并查询,因此请指出我的语法存在的问题:
Dim query As String = String.Empty
query &= "DECLARE @TaskID nvarchar(8), @Task nvarchar(50), @Start_date datetime, @Due_date datetime, @Complete bit, @Task_Manager nvarchar(8), @JRID nvarchar(10), @Entered_By char(50), @Time_Entered datetime;"
query &= "MERGE INTO schedule USING (VALUES (@TaskID, @Task, @start_date, @Due_Date, @Complete, @Task_Manager, @JRID, @Entered_By, @Time_Entered)) AS t(TaskID, Task, start_date, Due_Date, Complete, Task_Manager, JRID, Entered_By, Time_Entered) "
query &= "ON schedule.TaskID = @TaskID WHEN MATCHED THEN"
query &= " UPDATE SET schedule.TaskID = t.TaskID, schedule.Task=t.Task, schedule.start_date=t.start_date, schedule.due_date=t.due_date, schedule.complete=t.complete, schedule.task_manager=t.task_manager, "
query &= "schedule.JRID=t.JRID, schedule.Entered_by=t.Entered_by, schedule.Time_Entered=t.Time_Entered"
query &= " WHEN NOT MATCHED THEN INSERT (TaskID, Task, start_date, Due_Date, Complete, Task_Manager, JRID, Entered_By, Time_Entered)"
query &= " VALUES (@TaskID, @Task, @start_date, @Due_Date, @Complete, @Task_Manager, @JRID, @Entered_By, @Time_Entered);"
Using conn As New SqlConnection(dbLocations(0, 1))
Using comm As New SqlCommand()
With comm
For Each row As DataGridViewRow In MainSchedule.DataGridView1.Rows
If Not (row.Cells(0).Value = Nothing) Then
.Parameters.Clear()
.Connection = conn
.CommandType = CommandType.Text
.CommandText = query
insertcommand.Parameters.AddWithValue("@TaskID", TNn)
insertcommand.Parameters.AddWithValue("@Complete", "False")
insertcommand.Parameters.AddWithValue("@Task", row.Cells(0).Value)
insertcommand.Parameters.AddWithValue("@Start_date", row.Cells(1).Value)
insertcommand.Parameters.AddWithValue("@Due_Date", row.Cells(2).Value)
insertcommand.Parameters.AddWithValue("@JRID", txtJRID.Text)
insertcommand.Parameters.AddWithValue("@Task_Manager", row.Cells(3).Value)
insertcommand.Parameters.AddWithValue("@Entered_By", GetUserName())
insertcommand.Parameters.AddWithValue("@Time_Entered", Now)
NextTask()
End If
Next
End With
conn.Open()
comm.ExecuteNonQuery()
End Using
End Using
如果有人想知道,我想出来了,这是我的新代码:
Connexion.Open()
Dim query As String = String.Empty
Dim keypos = 0
query &= "UPDATE schedule SET Task = @Task, Complete = @Complete, Start_date = @Start_date, "
query &= "Due_date = @Due_date, JRID = @JRID, Task_Manager = @Task_Manager, Entered_By = @Entered_By, Time_Entered = @Time_Entered "
query &= "WHERE TaskID = @TaskID "
query &= "IF @@ROWCOUNT = 0 INSERT INTO schedule ( TaskID, Task, start_date, Due_Date, Complete, Task_Manager, JRID, Entered_By, Time_Entered)"
query &= " VALUES ( @TaskID, @Task, @start_date, @Due_Date, @Complete, @Task_Manager, @JRID, @Entered_By, @Time_Entered);"
For Each row As DataGridViewRow In MainSchedule.DataGridView1.Rows
If Not (row.Cells(0).Value = Nothing) Then
insertcommand.Parameters.Clear()
insertcommand.CommandText = query
insertcommand.Parameters.AddWithValue("@TaskID", row.Cells(0).Value)
insertcommand.Parameters.AddWithValue("@Complete", "False")
insertcommand.Parameters.AddWithValue("@Task", row.Cells(1).Value)
insertcommand.Parameters.AddWithValue("@Start_date", row.Cells(2).Value)
insertcommand.Parameters.AddWithValue("@Due_Date", row.Cells(3).Value)
insertcommand.Parameters.AddWithValue("@JRID", txtJRID.Text)
insertcommand.Parameters.AddWithValue("@Task_Manager", row.Cells(4).Value)
insertcommand.Parameters.AddWithValue("@Entered_By", GetUserName())
insertcommand.Parameters.AddWithValue("@Time_Entered", Now)
insertcommand.ExecuteNonQuery()
End If
keypos = keypos + 1
Next
Connexion.Close()