C#asp配置⽂件连接sql数据库
先引⽤
using System.Configuration;//配置⽂件
using System.Data.SqlClient;
我这⾥使⽤的是SqlServer 2008  sa ⽤户密码也为sa,开发⼯具为visual studio2012
前台代码:fig配置⽂件
1 <connectionStrings>
2    <!--<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-配置⽂件链接数据库-20150623105832;Integrated Security=SSPI;AttachDBFilename=|DataD
3    <add name="myconn" connectionString="Data Source=.;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=sa ;  Database=test1"/>
4  </connectionStrings>
后台代码:
using System;
using System.Collections.Generic;asp数据
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
//using MySql.Data.MySqlClient;//Mysql
using MySql.Data.MySqlClient;//Sql
using System.Configuration;//配置⽂件
using System.Data.SqlClient;
namespace配置⽂件链接数据库
{
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{  //获取配置⽂件中的连接字符串
string constr  =  ConfigurationManager.ConnectionStrings["myconn"].ToString();
using (SqlConnection con=new SqlConnection(constr)){
string selstr = "select name from user1 where 1=1";
SqlCommand com = new SqlCommand(selstr,con);
con.Open();
SqlDataReader sdr = com.ExecuteReader();
while (sdr.Read()){
ListBox1.Items.Add(sdr[0].ToString());
}
}
Response.Write(constr);
}
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。