目前,我正在使用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); }