How to fill DataTable with SQL Table

后端 未结 5 1130
太阳男子
太阳男子 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:24

    You can make method which return the datatable of given sql query:

    public DataTable GetDataTable()
    {
    SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["BarManConnectionString"].ConnectionString);
    conn.Open();
    string query = "SELECT * FROM [EventOne] ";
    
    SqlCommand cmd = new SqlCommand(query, conn);
    
    DataTable t1 = new DataTable();
    using (SqlDataAdapter a = new SqlDataAdapter(cmd))
    {
        a.Fill(t1);
    }
    return t1;
    }
    

    and now can be used like this:

    table = GetDataTable();
    

提交回复
热议问题