如何填写数据表与SQL表 [英] How to fill DataTable with SQL Table

查看:132
本文介绍了如何填写数据表与SQL表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在创建和读取与以下code的DataTable在我的Page_Load

 保护无效的Page_Load(对象发件人,EventArgs的)
{
    如果(会话[AllFeatures1] == NULL)
    {
        会话[AllFeatures1] =的GetData();
    }
    表=(数据表)会议[AllFeatures1];
    DayPilotCalendar1.DataSource =会话[AllFeatures1];
    DayPilotNavigator1.DataSource =会话[AllFeatures1];

    如果(!的IsPostBack)
    {
        的DataBind();
        DayPilotCalendar1.UpdateWithMessage(欢迎光临!);
    }

    如果(User.Identity.Name!=)
    {
        Panel1.Visible = TRUE;
    }
}
 

我想知道如何使之从一个SQL查询读取转换此code?我尝试用下面的code,但我不知道如何将它们连接起来,这样的数据表在我的网页加载填充下面的SQL命令。

 的SqlConnection康恩=新SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["BarManConnectionString"].ConnectionString);
conn.Open();
查询字符串=SELECT * FROM [EventOne];

的SqlCommand CMD =新的SqlCommand(查询,康涅狄格州);

数据表T1 =新的DataTable();
使用(SqlDataAdapter的一个=新的SqlDataAdapter(CMD))
{
    a.Fill(T1);
}
 

解决方案

您需要修改方法的GetData()并添加实验code有..和回报 T1 ..

完成。

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 (Session["AllFeatures1"] == null)
    {
        Session["AllFeatures1"] = GetData();
    }
    table = (DataTable)Session["AllFeatures1"];
    DayPilotCalendar1.DataSource = Session["AllFeatures1"];
    DayPilotNavigator1.DataSource = Session["AllFeatures1"];

    if (!IsPostBack)
    {
        DataBind();
        DayPilotCalendar1.UpdateWithMessage("Welcome!");
    }

    if (User.Identity.Name != "")
    {
        Panel1.Visible = true;
    }
}

I would like to know how to convert this code so that it reads from a SQL query? I am experimenting with the code below but I'm not sure how to connect them so that datatable in my page load fills with the SQL command below.

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);
}

解决方案

You need to modify the method GetData() and add your "experimental" code there.. and return t1..

Done.

这篇关于如何填写数据表与SQL表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆