1、在Web.config中配置
2、创建方法public static SqlConnection GetConnection()
{
string connectionString = ConfigurationManager.ConnectionStrings["数据库名称"].ConnectionString;
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
return connection;
}
3、通过SqlCommand执行
SqlConnection sqlconn = new SqlConnection(“server=.;uid=sa;pwd='';Integrated Security=True;);
你要简单的话,直接在.cs文件(即后台代码)中加入以下代码:(最后一句就建立了数据库连接)把这两句保存起来,以后哪个文件用到数据库连接的话就直接copy过去就行了
string sqlconnstr = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database.mdf;Integrated Security=True;User Instance=True";
SqlConnection sqlconn = new SqlConnection(sqlconnstr);