ASP.NET本地SQL Server数据库C#gridview数据绑定Visual Studio 2013 [英] ASP.NET local SQL Server database C# gridview data binding Visual Studio 2013
问题描述
如何将数据绑定到在Visual Studio 2013中使用C#背后的代码使用本地SQL Server数据库进行网格视图?
以下是我正在尝试使用的C#:
protected void Page_Load(object sender,EventArgs e)
{
SqlConnection con = new SqlConnection(@Data Source =(LocalDB )\v11.0; AttachDbFilename = C:\Users\Guillermo\Desktop\HorsensHospital\App_Data\HospitalDB.mdf; Integrated Security = True; Connect Timeout = 30);
con.Open();
SqlCommand cmd = new SqlCommand(SELECT [Task_temp_name],[Task_templatesID] FROM [Task_templates],con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
GridView1.DataSource = ds;
GridView1.DataBind();
cmd.ExecuteNonQuery();
con.Close();
code> DataSet 到你的Gridview的 DataSource
。
你需要使用 .Fill
方法来填充 SqlDataAdapter
的 DataSet
。您不需要使用 ExecuteNonQuery
。这个方法只是执行你的查询。它不会返回任何数据或结果。
另请使用 使用
语句来处理你的 SqlConnection
, SqlCommand
和 SqlDataAdapter的
。当您使用它时,您不需要使用 .Close()
数据库连接和对象。
使用(SqlConnection con = new SqlConnection(conString))
using(SqlCommand cmd = con.CreateCommand())
{
cmd.CommandText =SELECT [Task_temp_name ],[Task_templatesID] FROM [Task_templates];
using(SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataSet ds = new DataSet();
sda.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
I need some help because I've been trying different things but nothing seems to work properly the question itself is the one below.
How can I bind data to a grid-view in Visual Studio 2013 with a local SQL Server database using the code behind C# ?
Here is the C# I'm trying:
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Guillermo\Desktop\HorsensHospital\App_Data\HospitalDB.mdf;Integrated Security=True;Connect Timeout=30");
con.Open();
SqlCommand cmd = new SqlCommand("SELECT [Task_temp_name], [Task_templatesID] FROM [Task_templates]", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
GridView1.DataSource = ds;
GridView1.DataBind();
cmd.ExecuteNonQuery();
con.Close();
}
You just assing your empty DataSet
to your Gridview's DataSource
.
You need to use .Fill
method fo fill your DataSet
of SqlDataAdapter
. You don't need to use ExecuteNonQuery
. This method just executes your query. It doesn't return any data or something as a result.
Also use using
statement to dispose your SqlConnection
, SqlCommand
and SqlDataAdapter
. You don't need to .Close()
your database connections and objects when you use it.
using(SqlConnection con = new SqlConnection(conString))
using(SqlCommand cmd = con.CreateCommand())
{
cmd.CommandText = "SELECT [Task_temp_name], [Task_templatesID] FROM [Task_templates]";
using(SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataSet ds = new DataSet();
sda.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
这篇关于ASP.NET本地SQL Server数据库C#gridview数据绑定Visual Studio 2013的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!