asp.net 链接数据库ADO.NET

web.config

<configuration>
<connectionStrings>
<add name="constr" connectionString ="server=.\sqlexpress;database=db2016;uid=sa;pwd=123;"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>

</configuration>

 

 

 

default.aspx.cs

public partial class _Default : System.Web.UI.Page
{
private string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(constr);
SqlDataAdapter ad = new SqlDataAdapter("select * from tbuser", conn);
SqlCommandBuilder builder = new SqlCommandBuilder(ad);
DataTable dt = new DataTable();
ad.Fill(dt);
this.GridView1.DataSource = dt;
this.DataBind();
}
}

posted @ 2016-11-23 12:01  清风白水  阅读(123)  评论(0编辑  收藏  举报