VB.NET 无法更新 datagridview 中的 SQL 数据

Posted

技术标签:

【中文标题】VB.NET 无法更新 datagridview 中的 SQL 数据【英文标题】:VB.NET can not update SQL data in datagridview 【发布时间】:2017-11-10 21:37:40 【问题描述】:

我尝试了很多方法,但问题仍然无法解决。

我的目的是点击搜索按钮后,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

【问题讨论】:

【参考方案1】:

我花了一段时间才找到问题所在,因此我不得不重新编写它以匹配我通常做事的方式(如下所示)。您可以跳过这个冗长的 sn-p 到下面的解释,但我认为值得看看我将如何做到这一点,因为它确实解决了涉及日期范围的代码中的两个巨大问题。

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

第一个 sn-p 删除您之前设置的所有列,无论是通过代码还是设计器。你第一次没问题,因为你可以自动生成列。但是到第二次时,您已将其设置为False。现在你没有为网格定义列并且你告诉它不要自动生成它们。因此,您看不到任何结果。

【讨论】:

以上是关于VB.NET 无法更新 datagridview 中的 SQL 数据的主要内容,如果未能解决你的问题,请参考以下文章

关于VB.NET中 datagridview 刷新问题

数据集更新vb.net后DataGridView不刷新

VB.NET 2008 DataGridView 不更新 Visual Foxpro 数据库

VB.net 中的 DataGridView 不允许我更新

vb.net datagridview 无法添加行

无法使用 vb.net 将数据网格更新为 dbf