【问题标题】:How do I show a selected date data from a SQL Server database in a DataGridView?如何在 DataGridView 中显示来自 SQL Server 数据库的选定日期数据?
【发布时间】:2013-09-03 23:01:49
【问题描述】:

如何在DataGridView 中显示来自 SQL Server 数据库的选定日期数据?

我试过了,但它显示了数据库中的所有数据。

这是我点击按钮显示时触发的代码

SqlDataAdapter da = new SqlDataAdapter("select currDate,WtrNm,Type,No from WtrTblAllot", con);
DataSet ds = new DataSet();
da.Fill(ds);
dgvWtrAllot.DataSource = ds.Tables[0];

【问题讨论】:

    标签: c# sql-server winforms datagrid


    【解决方案1】:

    如@Coder 所说,在 WHERE 子句中使用您选择的日期...

    Date selectedDate = Date.Now;
    String query = "SELECT currDate,WtrNm,Type,No FROM WtrTblAllot WHERE currDate = @SelectedDate";
    SqlComman command = New SqlCommand(query, con);
    command.Parameters.AddWithValue("@SelectedDate", selectedDate);
    SqlDataAdapter da = new SqlDataAdapter(command);
    DataSet ds = new DataSet();
    da.Fill(ds);
    dgvWtrAllot.DataSource = ds.Tables[0];
    

    在查询中添加一些值时,更好的做法是使用参数...

    【讨论】:

      【解决方案2】:

      由于您的查询中没有WHERE 子句,因此很明显它将从数据库中获取所有数据。

      SqlDataAdapter da = new SqlDataAdapter("select currDate,WtrNm,Type,No
                                              from WtrTblAllot", con);
      

      如果您只想从数据库表中获取特定记录,请在查询中添加 WHERE 子句。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多