加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何使用SQL表填写DataTable

发布时间:2020-12-15 03:54:14 所属栏目:百科 来源:网络整理
导读:目前,我正在使用Page_Load中的以下代码创建和阅读DataTable protected void Page_Load(object sender,EventArgs e){ if (Session["AllFeatures1"] == null) { Session["AllFeatures1"] = GetData(); } table = (DataTable)Session["AllFeatures1"]; DayPilot
目前,我正在使用Page_Load中的以下代码创建和阅读DataTable
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;
    }
}

我想知道如何转换这个代码,以便它从SQL查询读取?我正在尝试下面的代码,但我不知道如何连接它们,以便我的页面加载中的datatable填充下面的SQL命令.

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

解决方法

您需要修改GetData()方法并在其中添加“实验”代码,并返回t1 ..

完成.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读