更新查询导致 vb.net 崩溃
Update query is causing a crash in vb.net
我正在尝试编写一个函数,该函数将 2 个索引作为参数并从具有该索引的 table 中获取值。使用这些值代码必须更新相同的 table。基本上是从 DataGridview 更新数据库。
没有任何效果,所以我注释掉了 SQL 行并使其非常非常简单。但这是另一个问题。它只是因为这个而崩溃 --> “'[13680] WindowsApp2.exe' 程序以代码 -1073740940 (0xc0000374) 退出。” (我把它翻译成英文)
什么都试过了请帮忙。顺便说一句,我查询的数据库在Ms Access上。
Private Function Update_Notes(rowIndex As Integer, colIndex As Integer)
If Environment.UserName = DataGridView1.Rows(DataGridView1.SelectedCells.Item(0).RowIndex).Cells.Item(1).Value.ToString() Then
'Get the values
con.Open()
Dim hakedKod As String
Dim ddate As Date
Dim note As String
Dim updateNoteId As Long
hakedKod = DataGridView2.Item(1, rowIndex).Value
ddate = DataGridView2.Item(2, rowIndex).Value
note = DataGridView2.Item(3, rowIndex).Value.ToString
updateNoteId = DataGridView2.Item(0, rowIndex).Value
Dim cmd As New OleDb.OleDbCommand
cmd.Connection = con
Dim sql As String
sql = "UPDATE TBL_Notes SET [Not]='12aa' WHERE [Kimlik] = 28"
'@hakedKod, [Tarih] = @ddate, [Not] = @note WHERE [Kimlik] = @id"
'initialize parameters for sterilization and date error stuff
cmd.Parameters.AddWithValue("@id", updateNoteId)
cmd.Parameters.AddWithValue("@hakedkod", hakedKod)
cmd.Parameters.AddWithValue("@ddate", ddate)
cmd.Parameters.AddWithValue("@note", note)
cmd.CommandText = sql
Dim i As Integer
Try
i = cmd.ExecuteNonQuery()
Catch ex As Exception
MsgBox(ex.Message)
Finally
DataGridView2_Loader()
End Try
con.Close()
Else
MsgBox("maalesef kayıt için değişiklik yetkiniz bulunmamaktadır.")
End If
End Function
函数具有 return 类型,包括关键字 Return
后跟您希望 return 的值。
我不喜欢巨大的 If
块,所以我否定了它并 returned 到调用代码。
连接应该在使用它们的方法中声明。它们必须被处置,命令也需要被处置。 Using...End Using
块负责关闭和处理,即使出现错误也是如此。
在 .Execute...
之前不要打开连接。
您的 CommandText
只包含文字;没有参数名称,因此参数值永远不会反映在执行中。
我不得不猜测参数的数据类型。检查你的数据库。我还不得不猜测 hakedkod 列的名称。我最接近 Google 翻译的是肉末。 :-)
Access 不注意参数名称。他们只对职位感兴趣。参数在 sql 字符串中的位置必须与参数添加到参数集合的顺序相匹配。
Private ConnStr As String = "Your connection string"
Private Function Update_Notes(rowIndex As Integer, colIndex As Integer) As Integer
If Environment.UserName <> DataGridView1.Rows(DataGridView1.SelectedCells.Item(0).RowIndex).Cells.Item(1).Value.ToString() Then
Return 0
End If
Dim i As Integer
Dim sql = "UPDATE TBL_Notes SET [Hak] = @hakedkod, [Tarih] = @ddate, [Not]= @note WHERE [Kimlik] = @id"
Using con As New OleDbConnection(ConnStr),
cmd As New OleDbCommand(sql, con)
cmd.Parameters.Add("@hakedkod", OleDbType.VarChar).Value = DataGridView2.Item(1, rowIndex).Value.ToString
cmd.Parameters.Add("@ddate", OleDbType.Date).Value = CDate(DataGridView2.Item(2, rowIndex).Value)
cmd.Parameters.Add("@note", OleDbType.VarChar).Value = DataGridView2.Item(3, rowIndex).Value.ToString
cmd.Parameters.Add("@id", OleDbType.Integer).Value = CInt(DataGridView2.Item(0, rowIndex).Value)
con.Open()
i = cmd.ExecuteNonQuery()
End Using
Return i
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim row As Integer
Dim col As Integer
Dim retVal As Integer
Try
retVal = Update_Notes(row, col)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
If retVal > 0 Then
DataGridView2_Loader()
Else
MsgBox("maalesef kayıt için değişiklik yetkiniz bulunmamaktadır.")
End If
End Sub
我正在尝试编写一个函数,该函数将 2 个索引作为参数并从具有该索引的 table 中获取值。使用这些值代码必须更新相同的 table。基本上是从 DataGridview 更新数据库。
没有任何效果,所以我注释掉了 SQL 行并使其非常非常简单。但这是另一个问题。它只是因为这个而崩溃 --> “'[13680] WindowsApp2.exe' 程序以代码 -1073740940 (0xc0000374) 退出。” (我把它翻译成英文)
什么都试过了请帮忙。顺便说一句,我查询的数据库在Ms Access上。
Private Function Update_Notes(rowIndex As Integer, colIndex As Integer)
If Environment.UserName = DataGridView1.Rows(DataGridView1.SelectedCells.Item(0).RowIndex).Cells.Item(1).Value.ToString() Then
'Get the values
con.Open()
Dim hakedKod As String
Dim ddate As Date
Dim note As String
Dim updateNoteId As Long
hakedKod = DataGridView2.Item(1, rowIndex).Value
ddate = DataGridView2.Item(2, rowIndex).Value
note = DataGridView2.Item(3, rowIndex).Value.ToString
updateNoteId = DataGridView2.Item(0, rowIndex).Value
Dim cmd As New OleDb.OleDbCommand
cmd.Connection = con
Dim sql As String
sql = "UPDATE TBL_Notes SET [Not]='12aa' WHERE [Kimlik] = 28"
'@hakedKod, [Tarih] = @ddate, [Not] = @note WHERE [Kimlik] = @id"
'initialize parameters for sterilization and date error stuff
cmd.Parameters.AddWithValue("@id", updateNoteId)
cmd.Parameters.AddWithValue("@hakedkod", hakedKod)
cmd.Parameters.AddWithValue("@ddate", ddate)
cmd.Parameters.AddWithValue("@note", note)
cmd.CommandText = sql
Dim i As Integer
Try
i = cmd.ExecuteNonQuery()
Catch ex As Exception
MsgBox(ex.Message)
Finally
DataGridView2_Loader()
End Try
con.Close()
Else
MsgBox("maalesef kayıt için değişiklik yetkiniz bulunmamaktadır.")
End If
End Function
函数具有 return 类型,包括关键字 Return
后跟您希望 return 的值。
我不喜欢巨大的 If
块,所以我否定了它并 returned 到调用代码。
连接应该在使用它们的方法中声明。它们必须被处置,命令也需要被处置。 Using...End Using
块负责关闭和处理,即使出现错误也是如此。
在 .Execute...
之前不要打开连接。
您的 CommandText
只包含文字;没有参数名称,因此参数值永远不会反映在执行中。
我不得不猜测参数的数据类型。检查你的数据库。我还不得不猜测 hakedkod 列的名称。我最接近 Google 翻译的是肉末。 :-)
Access 不注意参数名称。他们只对职位感兴趣。参数在 sql 字符串中的位置必须与参数添加到参数集合的顺序相匹配。
Private ConnStr As String = "Your connection string"
Private Function Update_Notes(rowIndex As Integer, colIndex As Integer) As Integer
If Environment.UserName <> DataGridView1.Rows(DataGridView1.SelectedCells.Item(0).RowIndex).Cells.Item(1).Value.ToString() Then
Return 0
End If
Dim i As Integer
Dim sql = "UPDATE TBL_Notes SET [Hak] = @hakedkod, [Tarih] = @ddate, [Not]= @note WHERE [Kimlik] = @id"
Using con As New OleDbConnection(ConnStr),
cmd As New OleDbCommand(sql, con)
cmd.Parameters.Add("@hakedkod", OleDbType.VarChar).Value = DataGridView2.Item(1, rowIndex).Value.ToString
cmd.Parameters.Add("@ddate", OleDbType.Date).Value = CDate(DataGridView2.Item(2, rowIndex).Value)
cmd.Parameters.Add("@note", OleDbType.VarChar).Value = DataGridView2.Item(3, rowIndex).Value.ToString
cmd.Parameters.Add("@id", OleDbType.Integer).Value = CInt(DataGridView2.Item(0, rowIndex).Value)
con.Open()
i = cmd.ExecuteNonQuery()
End Using
Return i
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim row As Integer
Dim col As Integer
Dim retVal As Integer
Try
retVal = Update_Notes(row, col)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
If retVal > 0 Then
DataGridView2_Loader()
Else
MsgBox("maalesef kayıt için değişiklik yetkiniz bulunmamaktadır.")
End If
End Sub