每当我运行它时,此异常将出现“nvarchar值'columnvalue'转换为数据类型int时转换失败。任何想法为什么?这也是一种永久性的删除行的方式吗?感谢您的未来建议!如何从datagridview中删除选定的行
Dim dataindex As Integer = Me.DataGridView1.CurrentRow.Index
Try
con = New SqlConnection
con.ConnectionString = Login.constring
Dim cmd As New SqlCommand("DELETE FROM tblmovies where movietitle = @a ", con)
cmd.Parameters.Add(New SqlParameter("@a", dataindex))
con.Open()
If MessageBox.Show("Do you really want to Delete this Record?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.Yes Then
cmd.ExecuteNonQuery()
con.Close()
Exit Sub
Else
con.Close()
Exit Sub
End If
Catch ex As SqlException
MessageBox.Show(ex.Message)
End Try
movietitle似乎更像是一个电影的名字,但你比较它从您的datagridview的currentrow得出的指数。你可能想把你的参数设置为在你的currentrow上保存你的movietitle的列值,或者改为传递该行的主键(如果你可以从你的currentrow访问它) –