1.新建一個ASP.NET網站模板;雙擊web.config文件,在<configuration>和</configuration>節點中添加一個<connectionStrings>節點,代碼如下:
- <connectionStrings>
- <add name="Con"
- connectionString="server=YAYUN\SQLEXPRESS;DataBase=Hotel;User ID=sa;Password=1111qq" />
- </connectionStrings>
sever是服務器名;DataBase是數據庫名;User ID是登錄用戶名;password是登錄密碼。
加入後整體代碼如下:
- <?xml version="1.0"?>
- <!--
- 有關如何配置 ASP.NET 應用程序的詳細信息,請訪問
- http://go.microsoft.com/fwlink/?LinkId=169433
- -->
- <configuration>
- <connectionStrings>
- <add name="Con"
- connectionString="server=YAYUN\SQLEXPRESS;DataBase=Hotel;User ID=sa;Password=1111qq" />
- </connectionStrings>
- <!--<connectionStrings>
- <add name="ApplicationServices"
- connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
- providerName="System.Data.SqlClient" />
- </connectionStrings>-->
- <system.web>
- <compilation debug="true" targetFramework="4.0" />
- <authentication mode="Forms">
- <forms loginUrl="~/Account/Login.aspx" timeout="2880" />
- </authentication>
- <membership>
- <providers>
- <clear/>
- <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
- enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
- maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
- applicationName="/" />
- </providers>
- </membership>
- <profile>
- <providers>
- <clear/>
- <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/>
- </providers>
- </profile>
- <roleManager enabled="false">
- <providers>
- <clear/>
- <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
- <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
- </providers>
- </roleManager>
- </system.web>
- <system.webServer>
- <modules runAllManagedModulesForAllRequests="true"/>
- </system.webServer>
- </configuration>
雙擊解決方案資源管理器下的Default.aspx.cs,編寫如下代碼:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Data.SqlClient;
- namespace WebApplication7
- {
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- SqlConnection connecton = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["Con"].ConnectionString.ToString());
- try
- {
- connecton.Open();
- this.Label1.Text = "連接數據庫測試成功!";
- }
- catch (Exception err)
- {
- this.Label1.Text = "連接數據庫失敗!";
- Label1.Text += err;
- }
- finally
- {
- connecton.Close();
- }
- }
- }
- }
- 調試結果如下:
連接不成功一般問題出在web.config文件的代碼,即
- <connectionStrings>
- <add name="Con"
- connectionString="server=YAYUN\SQLEXPRESS;DataBase=Hotel;User ID=sa;Password=1111qq" />
- </connectionStrings>