【问题标题】:display database table data in datatable in c# but the datatable is blank在c#中的datatable中显示数据库表数据但datatable为空
【发布时间】:2017-06-14 06:19:14
【问题描述】:

我有一个GridView。我正在尝试使用DataTable 将数据库表数据显示到网格中。所以我把查询结果保存在DataTable,但是数据没有显示出来。这是我的代码。请帮忙。

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {        
        DataTable dataTable = new DataTable();
        string constr = ConfigurationManager.ConnectionStrings["gridconnection"].ConnectionString;

        string query = "select * from GridExcel";

        SqlConnection con1 = new SqlConnection(constr);
        SqlCommand cmd = new SqlCommand(query, con1);
        con1.Open();

        // create data adapter
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        // this will query your database and return the result to your 
        datatable
        da.Fill(dataTable);
        Gridview1.DataSource = dataTable;
        Gridview1.DataBind();
        ViewState["CurrentTable"] = dataTable;
        con1.Close();
    }
}

【问题讨论】:

  • con.Open();还是 con1.Open()?
  • select * from GridExcel 有什么回报吗?
  • SqlDataAdapter 不需要打开连接
  • 至少编译一次你的代码,并确保你帖子中的代码没有任何编译时错误
  • @SureshPrajapati 我没有收到任何编译时错误

标签: c# gridview datatable


【解决方案1】:

尝试使用 DataReader:

protected void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack)
  {

    DataTable dataTable = new DataTable();
    string constr =      ConfigurationManager.ConnectionStrings["gridconnection"].ConnectionString;

    string query = "select * from GridExcel";

    SqlConnection con1 = new SqlConnection(constr);
    SqlCommand cmd = new SqlCommand(query, con1);
    con1.Open();

    // create data adapter
    SqlDataReader reader = cmd.ExecuteReader();
    // this will query your database and return the result to your datatable
    dataTable.Load(reader);
    Gridview1.DataSource = dataTable;
    Gridview1.DataBind();
    ViewState["CurrentTable"] = dataTable;
    con1.Close();
  }
}

【讨论】:

  • 没有人问我那里的解释/澄清,在这里我怀疑为什么读者会在适配器失败的情况下做这项工作,这就是我要求澄清的原因。如果您需要对我的任何帖子进行澄清,则可以通过评论或邮件免费询问。
  • 我对争论不感兴趣,并采取个人报复,请忽略我的评论(让我也删除)。抱歉浪费时间
  • 澄清一下:据我了解,如果列已经存在,SqlDataAdapter 会填充一个表 (msdn.microsoft.com/de-de/library/905keexk(v=vs.110).aspx)
  • @RomanoZumbé 我的代码的结果和你的代码是一样的。它显示第一列数据,但不显示剩余列数据。
【解决方案2】:

下面的代码对我有用。

string constring = @"Data Source=.\SQL2005;Initial Catalog=gridconnection;";
    using (SqlConnection con = new SqlConnection(constring))
    {
        using (SqlCommand cmd = new SqlCommand("select * from GridExcel", con))
        {
            cmd.CommandType = CommandType.Text;
            using (SqlDataAdapter da = new SqlDataAdapter(cmd))
            {
                using (DataTable dt = new DataTable())
                {
                    da.Fill(dt);
                    dataGridView1.DataSource = dt;
                }
            }
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-26
    相关资源
    最近更新 更多