我在mysql中创建了一个表,其中包含属性产品代码,数量,公司,价格。我在vb 2012中创建了一个datagridview,我想从表单中获取输入,然后在datagridview中显示结果。我还想从我在mysql中创建的表中显示价格。但是,我无法这样做。
这是我的程序的代码。请帮助我
Dim row As Integer = DataGridView1.Rows.Add()
Dim connection As String
Dim command As String
Dim command2 As String
command2 = "select Company from Stock WHERE Product_Code =('" + TextBox1.Text + "');"
connection = "Data Source=localhost; Database=Entry; User Id=root; Password=;"
command = "select Price from Stock WHERE Product_Code =('" + TextBox1.Text + "');"
Dim con As New MySqlConnection(connection)
Dim cmd As New MySqlCommand(command)
Dim data As DataTable
Dim adp As New MySqlDataAdapter
Dim data2 As DataTable
Dim adp2 As New MySqlDataAdapter
DataGridView1.Rows.Item(row).Cells(0).Value = TextBox1.Text
DataGridView1.Rows.Item(row).Cells(2).Value = TextBox2.Text
Try
adp = New MySqlDataAdapter(command, connection)
adp2 = New MySqlDataAdapter(command2, connection)
data = New DataTable
data2 = New DataTable
adp.Fill(data)
adp2.Fill(data2)
DataGridView1.Rows.Item(row).Cells(1).Value = data
DataGridView1.Rows.Item(row).Cells(3).Value = data
Catch ex As Exception
MessageBox.Show("Error")
End Try
答案 0 :(得分:0)
您应该能够找到如何在SO(堆栈溢出)中执行此操作的示例。但是,为了帮助您,您需要研究以下内容:
首先,您应该参数化SQL以防止注入和可读性:http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters.aspx
其次,您不向数据网格添加行,您将数据源设置为实现IList的内容:http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.datasource.aspx然后,如果需要,可以将项目添加到列表中。如果您只想显示表中的行,可以将数据源设置为数据表(DATA)。