2017-11-10 77 views
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 

回答

0

我花了一段時間來發現問題,並在這樣做,我不得不重新寫的方式我通常會匹配做事情(下面)。你可以直接跳過下面的解釋,但我認爲這是值得的,因爲它確實解決了代碼中涉及日期範圍的兩個巨大問題。

Private Sub search_btn_Click(sender As Object, e As EventArgs) Handles search_btn.Click 
    Dim sql As String = "SELECT * FROM PRODUCT WHERE 1=1" 
    Dim connectionString As String = "Data Source=xxx.xx.xx.xxx;Initial Catalog=QCDB;User ID=sa;Password=xxxxx;" 

    Try 

     Using connection As New SqlConnection(connectionString), _ 
       cmd As New SqlCommand(sql, connection) 

      'Match each of the parameter types and lengths to the columns 
      If sample_source_combox.Text <> "all" Then 
       cmd.CommandText += " AND SAMPLE_SOURCE = @SOURCE" 
       cmd.Paramters.Add("@SOURCE", SqlDbType.NVarChar, 15).Value = sample_source_combox.Text 
      End If 

      If product_type_combox.Text <> "all" Then 
       cmd.CommandText += " AND PRO_TYPE = @TYPE" 
       cmd.Parmeters.Add("@TYPE", SqlDbType.NVarChar, 15).Value = product_type_combox.Text 
      End If 

      If product_name_combox.Text <> "all" Then 
       cmd.CommandText += " AND PRO_NAME = @NAME" 
       cmd.Parmeters.Add("@Name", SqlDbType.NVarChar, 60).Value = product_name_combox.Text 
      End If 

      If embossing_top_combox.Text <> "all" Then 
       cmd.CommandText += " AND EMBOSS_TOP = @EMBOSS_TOP" 
       cmd.Parameters.Add("@EMBOSS_TOP", SqlDbType.NVarChar, 20).Value = embossing_top_combox.Text 
      End If 

      If embossing_bottom_combox.Text <> "all" Then 
       cmd.CommandText += " AND EMBOSS_BOTTOM = @EMBOSS_BOTTOM" 
       cmd.Parameters.Add("@EMBOSS_BOTTOM", SqlDbType.NVarChar, 20).Value = embossing_top_combox.Text 
      End If 

      If line_number_combox.Text <> "all" Then 
       cmd.CommandText += " AND LINE_NO = @LINE_NO" 
       cmd.Parameters.Add("@LINE_NO", SqlDbtype.Int).Value = Integer.Parse(line_number_combox.Text) 
      End If 

      If shift_search_combox.Text <> "all" Then 
       cmd.CommandText += " AND SHIFT = @SHIFT" 
       cmd.Parameters.Add("@SHIFT", SqlDbType.NVarChar, 10).Value = shift_search_combox.Text 
      End If 

      'This should probably be a RANGE of thicknesses, just like the dates. 
      If std_thickness_combox.Text <> "all" Then 
       cmd.CommandText += " AND STD_THICK = @STAND_THICKNESS" 
       cmd.Parameters.Add("@STAND_THICKNESS", SqlDbType.Float).Value = Double.Parse(std_thickness_combox.Text) 
      End If 

      If color_search_combox.Text <> "all" Then 
       cmd.CommandText += " AND COLOR = @COLOR" 
       cmd.Parameters.Add("@COLOR", SqlDbType.NVarChar, 15).Value = color_search_combox.Text 
      End If 

      If Not String.IsNullOrWhiteSpace(FROMTIME) THEN 
       cmd.CommandText += " AND PRO_DATE >= @FROMTIME" 
       cmd.Parameters.Add("@FROMTIME", SqlDbType.DateTime).Value = DateTime.Parse(FROMTIME) 
      End If 

      If Not String.IsNullOrWhiteSpace(TOTIME) THEN 
       cmd.CommandText += " AND PRO_DATE < @TOTIME" 
       cmd.Parameters.Add("@TOTIME", SqlDbType.DateTime).Value = DateTime.Parse(TOTIME) 
      End If 

      Dim dataadapter As New SqlDataAdapter(cmd) 
      Dim ds As New System.Data.DataSet() 
      dataadapter.Fill(ds, "PRODUCT") 
     End Using 

     With Me.DataGridView1 
      .RowsDefaultCellStyle.BackColor = Color.AliceBlue 
      .AlternatingRowsDefaultCellStyle.BackColor = Color.White 
     End With 

     DataGridView1.DataSource = ds.Tables(0) 
     DataGridView1.AutoGenerateColumns = False 

    Catch ex As Exception 
     MessageBox.Show(ex.ToString) 
    End Try 
End Sub 

現在解釋爲什麼你第二次沒有得到結果。運行此方法時,你這樣做:

DataGridView1.Columns.Clear() 

但是你也這樣做:

DataGridView1.AutoGenerateColumns = False 

這第一個片段刪除任何列在設置之前,無論是代碼或由設計師。你第一次沒事,因爲你可以自動生成列。但通過第二次,您已將此設置爲False。現在,您沒有爲網格定義的列,您告訴它不要自動生成它們。因此你看不到任何結果。