问题
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();
}
回答1:
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();
}
}
回答2:
You should use statement like this : when you use SqlDataAdapter you should fill an dataset with Fill Method then set the DataSource Properties Of GridView
SqlCommand cmd = new SqlCommand("SELECT [Task_temp_name], [Task_templatesID] FROM [Task_templates]", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
con.Close();
回答3:
You have missed Fill
method to fill your your dataset that is why i think you are not able to display gridview
.
Fill Method is very Improtant method Bcoz whatever your query is returning that should be filled into your dataset & then that dataset is binded by your Gridiview
Hope this will help you
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();
sda.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
cmd.ExecuteNonQuery();
con.Close();
}
Note : You need to create your connection string in Web.Config. Bcoz If you have created your connection string there then you will not need to create that again. Once creating there you will just need to call that from the Web.config
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringNameFromWebConfig"].ConnectionString);
This is the syntax for calling connection string from web.config
来源:https://stackoverflow.com/questions/26817272/asp-net-local-sql-server-database-c-sharp-gridview-data-binding-visual-studio-20