本文介绍了如何用SQL表填充DataTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在使用 Page_Load 中的以下代码创建和读取数据表
I am currently creating and reading a DataTable with the following code in my Page_Load
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 命令.
I would like to know how to convert this code so that it reads from a SQL query? I am experimenting with the code below but I'm not sure how to connect them so that datatable in my page load fills with the SQL command below.
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];
推荐答案
您需要修改方法 GetData()
并在那里添加您的实验"代码,并返回 t1代码>.
You need to modify the method GetData()
and add your "experimental" code there, and return t1
.
这篇关于如何用SQL表填充DataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!