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();
adapter.SelectCommand = cmd;
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();
adapter.SelectCommand = cmd;
DataSet ds=new DataSet();
adapter.Fill(ds);
return ds;
}
}
}