请帮我解决这个问题,我已经搜索了几个小时但无法解决。
这是我的代码:
Private Sub search_btn_Click(sender As Object, e As EventArgs) Handles search_btn.Click
connectionString = "Data Source=MININT-BJDM4MI;AttachDbFilename=C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\QCDB.mdf;Database=QCDB;Trusted_Connection=Yes;"
sql = "SELECT * FROM PRODUCT WHERE SAMPLE_SOURCE = @SOURCE"
Dim connection As New SqlConnection(connectionString)
Dim dataadapter As New SqlDataAdapter(sql, connection)
Dim ds As New System.Data.DataSet()
Dim cmd = New SqlCommand(sql, connection)
cmd.Parameters.AddWithValue("@SOURCE", spl_source_combox.Text)
Try
connection.Open()
dataadapter.Fill(ds, "PRODUCT")
DataGridView1.Refresh()
cmd.ExecuteNonQuery()
connection.Close()
DataGridView1.DataSource = ds.Tables(0)
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
答案 0 :(得分:0)
如果您使用存储过程而不是在代码中传递sql会更好,但是您的异常的原因是因为@SOURCE变量未在您的SQL语句中声明。试试这个:
UIAlertViewController
答案 1 :(得分:0)
创建SqlDataAdapter时,应该传入命令对象而不是SQL字符串。
Private Sub search_btn_Click(sender As Object, e As EventArgs) Handles search_btn.Click
connectionString = "Data Source=MININT-BJDM4MI;AttachDbFilename=C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\QCDB.mdf;Database=QCDB;Trusted_Connection=Yes;"
sql = "SELECT * FROM PRODUCT WHERE SAMPLE_SOURCE = @SOURCE"
Dim connection As New SqlConnection(connectionString)
Dim cmd = New SqlCommand(sql, connection)
Dim dataadapter As New SqlDataAdapter(cmd)
Dim ds As New System.Data.DataSet()
cmd.Parameters.AddWithValue("@SOURCE", spl_source_combox.Text)
Try
connection.Open()
dataadapter.Fill(ds, "PRODUCT")
DataGridView1.Refresh()
connection.Close()
DataGridView1.DataSource = ds.Tables(0)
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
答案 2 :(得分:0)
您需要为@source分配值:使用此行
cmd.Parameters.AddWithValue("@SOURCE", spl_source_combox.Text) = "<Your Value>"