回答

收藏

如何用SQL表填充DataTable

技术问答 技术问答 45 人阅读 | 0 人回复 | 2023-09-13

我目前正在使用它Page_Load创建和读取以下代码DataTable
! a8 f* [: s- Cprotected 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命令填充。2 z' {/ X5 V% l8 L" W5 T
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);}我被困在:
  C3 A# @0 U- z- }+ s5 f% |table = (DataTable)Session["AllFeatures1"];我想成为 t1 = (DataTable)Session["AllFeatures1];" ]- G5 X7 _6 O( t  L
                                                               
  s* \: s. T, q8 P% Y3 X  a    解决方案:                                                               
( f$ a' v/ \0 c: d- P  k                                                                需要修改方法,GetData()并在其中添加实验代码,然后返回t1。
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则