How to fill DataTable with SQL Table

后端 未结 5 1128
太阳男子
太阳男子 2020-12-10 12:57

I am currently creating and reading a DataTable with the following code in my Page_Load

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


        
5条回答
  •  旧巷少年郎
    2020-12-10 13:26

    The SqlDataReader is a valid data source for the DataTable. As such, all you need to do its this:

    public DataTable GetData()
    {
        SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["BarManConnectionString"].ConnectionString);
        conn.Open();
        string query = "SELECT * FROM [EventOne]";
        SqlCommand cmd = new SqlCommand(query, conn);
    
        DataTable dt = new DataTable();
        dt.Load(cmd.ExecuteReader());
        conn.Close();
        return dt;
    }
    

提交回复
热议问题