将ASP.NET(2010)与MSSQL连接

时间:2019-03-22 06:41:31

标签: c# asp.net sql-server

string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))//automatic closing of connection- advantage
{
    SqlCommand cmd = new SqlCommand("SELECT * FROM SalesLT.ImpAddress", con);
    con.Open();
    GridView1.DataSource(cmd.ExecuteReader().ToString());
    GridView1.DataBind();
}

在DataSource上显示错误。显示的错误是:

  

错误1不可使用的成员'System.Web.UI.WebControls.BaseDataBoundControl.DataSource'不能像方法一样使用。

1 个答案:

答案 0 :(得分:0)

尝试一下:

string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))//automatic closing of connection- advantage
{
    SqlCommand cmd = new SqlCommand("SELECT * FROM SalesLT.ImpAddress", con);
    con.Open();
    GridView1.DataSource=cmd.ExecuteReader();
    GridView1.DataBind();
}