-4
try
        {
            SqlCommand cmd = new SqlCommand(SELECT [Name], COUNT([Name]) AS Count FROM [DelegationTracker] WHERE [Date] = @Date GROUP BY [Name], Connection);Conn);
             SqlCommand cmd = new SqlCommand(SELECT @User_Tested FROM WHMIS_User_Tests;, Connct);
            cmd.Parameters.Clear();
            cmd.Parameters.Add(@Name, SqlDbType.VarChar).Value = HttpContext.Current.Session[Name].ToString();
            cmd.Parameters.Add(@Date, SqlDbType.VarChar).Value = Date;MMDDYYYY
             cmd.Parameters.Add(@Grade, SqlDbType.VarChar).Value = null;
            Connection.Open();

            using (SqlDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    test = reader.GetValue(0).ToString();
                    test2 = reader.GetValue(1).ToString();
                }

            }

            SqlDataReader dr = cmd.ExecuteReader();
            SqlDataAdapter Adapter = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            dt.Load(dr);
            Adapter.Fill(dt);
             DataSet ds = new DataSet();
            Adapter.Fill(ds);
            MainGridView.DataSource = dt;
            MainGridView.DataSource = ds;
            MainGridView.DataBind();
            Connection.Close();
            return true;
}

假设它没有数据,gridview 不显示即时消息,并且 DataTable 返回 SQL 表中的 1 条记录,但不显示它(在 gridview 中)据我所知,数据集什么也不返回

我有这么多适配器、数据集和数据表的原因是我正在测试看看什么有效,但没有一个产生任何结果......

4

1 回答 1

0

我不太确定您的示例代码中发生了什么。看起来您几乎采用了两种不同的方法并将它们混在一起。这是一个简单的方法或使用您提供的两个 SQL 查询之一加载 DataGrid。

private void LoadDataGrid(DataGrid dataGrid, DateTime date, string connectionString) 
{
    using (var conn = new SqlConnection(connectionString)) 
    {
        using (SqlCommand cmd = conn.CreateCommand()) 
        {
            cmd.Parameters.Add(new SqlParameter {
                ParameterName = "@Date",
                SqlDbType = SqlDbType.DateTime,
                Value = date
            });
            cmd.CommandText = "SELECT [Name], " +
                              "       COUNT([Name]) AS Count " +
                              "FROM   [DelegationTracker] " +
                              "WHERE  [Date] = @Date " +
                              "GROUP  BY [Name]";

            conn.Open();
            try 
            {
                using (SqlDataReader reader = cmd.ExecuteReader()) 
                {
                    dataGrid.DataSource = reader;
                    dataGrid.DataBind();
                }
            }
            finally 
            {
                conn.Close();
            }
        }
    }
}
于 2013-05-08T13:56:33.180 回答