熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

ADO.Net 連接數據庫示例

2022-06-13   來源: .NET編程 

  連接本地SQL Server數據庫學習示例

  配置nfig

   <connectionStrings>

       <add name=We_CMS connectionString=Data Source=ZhangNanPC; Initial Catalog=We_CMS ; Integrated Security=SSPI/>

     </connectionStrings>

  寫一個測試連接的web form

   <body>

       <form id=form runat=server>

       <div>

           <asp:Label runat=server ID=lblInfo></asp:Label>

       </div>

       </form>

   </body>

   protected void Page_Load(object sender EventArgs e)

       {

           string connectionString = WebConfigurationManagerConnectionStrings[We_CMS]ConnectionString;

           SqlConnection con = new SqlConnection(connectionString)

  

           try

           {

               conOpen()

               lblInfoText = <b>Version : </b>+conServerVersion;

               lblInfoText += <br /><b>Connection Is:</b> + conStateToString()

           }

           catch (Exception err)

           {

               lblInfoText = Error reading the database + errMessage;

           }

           finally

           {

               conClose()

               lblInfoText += <br /><b> Now Connection Is : </b> + conStateToString()

           }

  

  

       }

  測試結果如下

  Version :

  Connection Is:Open

  Now Connection Is : Closed


From:http://tw.wingwit.com/Article/program/net/201311/11353.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.