Sql数据库连接配置
时间:2011-04-18 来源:happygx
Web config配置连接字符串
<connectionStrings>
<add name="DBName" connectionString="data source='服务器名';User ID='用户名';Password='密码' ;Initial Catalog='数据库名'">
</connectionStrings>
public static DataSet(string Sql)
{
using(SqlConnection conn=new SqlConnection(ConfigurationManager.ConnectionStrings["DBName"].ConnectionString))
{
conn.Open();
using(SqlCommand cmd=conn.CreateCommand())
{
cmd.CommandText=Sql;
SqlDataAdapter adapter=new SqlDataAdapter();
DataSet ds=new DataSet();
adapter.Fill(ds);
return ds;
}
}
}
直接添加字符串连接
public static DataSet(string Sql){
using(SqlConnection conn=new SqlConnection(@"data source="服务器名";User ID="用户名";Password="密码";Initial Catalog="数据库名""))
{
conn.Open();
using(SqlCommand cmd=conn.CreateCommand())
{
cmd.CommandText=Sql;
SqlDataAdapter adapter=new SqlDataAdapter();
DataSet ds=new DataSet();
adapter.Fill(ds);
return ds;
}
}
}