数据库的连接方法有2
1、在web.config 文件添加<appseting>
<add key="ConnectionString" value="uid=;password=;database=;server=;"> </appseting>
调用时:
string connectionString= System.Configuration.ConfigurationManager.AppSetings["ConnectionString"].ToString();
2、在web.config 文件添加<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=;Inital Catalog=; Integrated Security=True;" ProviderName="System.Data.SqlClinet"> </connectionStrings>
3、创建“基于服务的数据库”
string dataDir = AppDomain.CurrentDomain.BaseDirectory;//获取应用程序当前目录 if(dataDir.EndsWith(@"\bin\Debug\")|| dataDir.EndsWith(@"\bin\Release\"))//此方法适用于控制台应用和winForm { dataDir=System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName; AppDomain.CurrentDomain.SetData("DataDirectory",dataDir); } string strConnection = @"Data Source=.\SQLEXPRESS;AttachDBFileName=|DataDirectory|\Database1.mdf; Integrated Security=True;User Instance=True";