我目前正在使用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查询中读取?我正在尝试下面的代码,但不确定如何连接它们,以便页面加载中的数据表使用下面的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];
t1 = (DataTable)Session["AllFeatures1];
您需要修改方法,GetData()并在其中添加“实验”代码,然后返回t1。
GetData()
t1