如何从SQL表填充网格视图

本文关键字:填充 网格 视图 SQL | 更新日期: 2023-09-27 18:07:42

我试图通过连接字符串从SQL表中的查询填充网格视图,但不知何故我收到以下错误。实际上,作为一个初学者,我认为这将是一个极其简单的代码错误,所以不要假设任何事情!

"系统找不到指定的文件

描述:在执行过程中发生未处理的异常当前的web请求。请查看堆栈跟踪了解更多信息有关错误及其在代码中的起源的信息。

异常详细信息:System.ComponentModel。Win32Exception:系统异常找不到指定的文件

下面是我的代码:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data.Odbc;
namespace GridviewTOExcel
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("data source=.; database=Richard2016DB; integrated security=SSPI");
            SqlCommand cmd = new SqlCommand("Select * from dbo.tblEmployee", con);
            con.Open();
            SqlDataReader rdr = cmd.ExecuteReader();
            GridView1.DataSource = rdr;
            GridView1.DataBind();
            con.Close();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
        }
    }
}

如何从SQL表填充网格视图

我认为你应该先把它发送给DataTable,然后才能绑定它。

protected void Page_Load(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection("data source=.; database=Richard2016DB; integrated security=SSPI");
    SqlCommand cmd = new SqlCommand("Select * from dbo.tblEmployee", con);
    con.Open();
    DataTable table = New DataTable();
    using(SqlDataAdapter da = new SqlDataAdapter(cmd))
        da.Fill(table);
    GridView1.DataSource = table;
    GridView1.DataBind();
    con.Close();
}

你需要下面的引用来使用DataTable的

using System.Data;
using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
    namespace GridviewTOExcel
    {
      public partial class WebForm1 : System.Web.UI.Page
      {
        private SqlConnection con;
        private SqlCommand cmd;
        private string constr, query;
        private void connection()
        {
            string dbConnectiomName = "conStr"; // Set your keyname from web.config file
            constr = WebConfigurationManager.ConnectionStrings[dbConnectiomName].ToString();
            con = new SqlConnection(constr);
            con.Open();
        }
         protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                gvBind(); // Bind Gridview on pageload
            }
        }
        // Function which bind gridview control  
        Public void gvBind(){
           connection();
            cmd = new SqlCommand();
            cmd.CommandText = "Select Query"; // best practise is to use storedprocedure
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            cmd.CommandTimeout = 0;
            SqlDataAdapter dap = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            dap.Fill(ds);
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
        }
     }
    }