0
我嘗試了很多方法,但問題依然無法解決。VB.NET無法更新datagridview中的SQL數據
我的目的是點擊搜索按鈕後,datagridview會在一定條件下顯示來自SQL數據庫的數據。
當我第一次點擊搜索按鈕,它的作品。但後來我點擊搜索按鈕第二次datagridview顯示空白(我改變了一些條件,如不同的行號等)。
請幫幫我!謝謝你這麼多
這裏是我的VB代碼:
Private Sub search_btn_Click(sender As Object, e As EventArgs) Handles search_btn.Click
Dim connectionString As String = "Data Source=xxx.xx.xx.xxx;Initial Catalog=QCDB;User ID=sa;Password=xxxxx;"
'different content of FROMTIME AND TOTIME
If FROMTIME = " " And TOTIME = " " Then
sql = "SELECT * FROM PRODUCT WHERE (SAMPLE_SOURCE = @SOURCE OR @SOURCE = 'all') AND (PRO_TYPE = @TYPE OR @TYPE = 'all') AND (PRO_NAME = @NAME OR @NAME = 'all')" &
"AND (EMBOSS_TOP = @EMBOSS_TOP OR @EMBOSS_TOP = 'all') AND (EMBOSS_BOTTOM = @EMBOSS_BOTTOM OR @EMBOSS_BOTTOM = 'all') AND (LINE_NO = @LINE_NO OR @LINE_NO = 'all')" &
"AND (SHIFT = @SHIFT OR @SHIFT = 'all') AND (STD_THICK = @STAND_THICKNESS OR @STAND_THICKNESS = 'all') AND (COLOR = @COLOR OR @COLOR = 'all') "
ElseIf FROMTIME = " " And TOTIME <> " " Then
FROMTIME = DateTime.Now.AddYears(-15).ToString("yyyy-MM-dd")
sql = "SELECT * FROM PRODUCT WHERE (SAMPLE_SOURCE = @SOURCE OR @SOURCE = 'all') AND (PRO_TYPE = @TYPE OR @TYPE = 'all') AND (PRO_NAME = @NAME OR @NAME = 'all')" &
"AND (EMBOSS_TOP = @EMBOSS_TOP OR @EMBOSS_TOP = 'all') AND (EMBOSS_BOTTOM = @EMBOSS_BOTTOM OR @EMBOSS_BOTTOM = 'all') AND (LINE_NO = @LINE_NO OR @LINE_NO = 'all')" &
"AND (SHIFT = @SHIFT OR @SHIFT = 'all') AND (STD_THICK = @STAND_THICKNESS OR @STAND_THICKNESS = 'all') AND (COLOR = @COLOR OR @COLOR = 'all') AND PRO_DATE BETWEEN'" & FROMTIME & "' AND '" & TOTIME & "'"
ElseIf FROMTIME <> " " And TOTIME = " " Then
TOTIME = DateTime.Now.ToString("yyyy-MM-dd")
sql = "SELECT * FROM PRODUCT WHERE (SAMPLE_SOURCE = @SOURCE OR @SOURCE = 'all') AND (PRO_TYPE = @TYPE OR @TYPE = 'all') AND (PRO_NAME = @NAME OR @NAME = 'all')" &
"AND (EMBOSS_TOP = @EMBOSS_TOP OR @EMBOSS_TOP = 'all') AND (EMBOSS_BOTTOM = @EMBOSS_BOTTOM OR @EMBOSS_BOTTOM = 'all') AND (LINE_NO = @LINE_NO OR @LINE_NO = 'all')" &
"AND (SHIFT = @SHIFT OR @SHIFT = 'all') AND (STD_THICK = @STAND_THICKNESS OR @STAND_THICKNESS = 'all') AND (COLOR = @COLOR OR @COLOR = 'all') AND PRO_DATE BETWEEN'" & FROMTIME & "' AND '" & TOTIME & "'"
Else
sql = "SELECT * FROM PRODUCT WHERE (SAMPLE_SOURCE = @SOURCE OR @SOURCE = 'all') AND (PRO_TYPE = @TYPE OR @TYPE = 'all') AND (PRO_NAME = @NAME OR @NAME = 'all')" &
"AND (EMBOSS_TOP = @EMBOSS_TOP OR @EMBOSS_TOP = 'all') AND (EMBOSS_BOTTOM = @EMBOSS_BOTTOM OR @EMBOSS_BOTTOM = 'all') AND (LINE_NO = @LINE_NO OR @LINE_NO = 'all')" &
"AND (SHIFT = @SHIFT OR @SHIFT = 'all') AND (STD_THICK = @STAND_THICKNESS OR @STAND_THICKNESS = 'all') AND (COLOR = @COLOR OR @COLOR = 'all') AND PRO_DATE BETWEEN'" & FROMTIME & "' AND '" & TOTIME & "'"
End If
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", sample_source_combox.Text)
cmd.Parameters.AddWithValue("@TYPE", product_type_combox.Text)
cmd.Parameters.AddWithValue("@NAME", product_name_combox.Text)
cmd.Parameters.AddWithValue("@EMBOSS_TOP", embossing_top_combox.Text)
cmd.Parameters.AddWithValue("@EMBOSS_BOTTOM", embossing_bottom_combox.Text)
cmd.Parameters.AddWithValue("@LINE_NO", line_number_combox.Text)
cmd.Parameters.AddWithValue("@SHIFT", shift_serach_combox.Text)
cmd.Parameters.AddWithValue("@STAND_THICKNESS", std_thickness_combox.Text)
cmd.Parameters.AddWithValue("@COLOR", color_search_combox.Text)
Try
connection.Open()
dataadapter.Fill(ds, "PRODUCT")
DataGridView1.Columns.Clear()
DataGridView1.Refresh()
With Me.DataGridView1
.RowsDefaultCellStyle.BackColor = Color.AliceBlue
.AlternatingRowsDefaultCellStyle.BackColor = Color.White
End With
connection.Close()
DataGridView1.DataSource = ds.Tables(0)
DataGridView1.AutoGenerateColumns = False
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub