我目前在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);
}
我卡在这里:
table = (DataTable)Session["AllFeatures1"];
我希望这样做:t1 = (DataTable)Session["AllFeatures1];