Store Your Database Connection String in Web.Config...

原创 2004年10月06日 20:57:00
By: John Kilgo spacer.gif Date: November 28, 2002spacer.gifdownload.gifspacer.gifDownload the code. spacer.gif <?XML:NAMESPACE PREFIX = ASP />spacer.gifPrinter Friendly Version

Here we present an example of how to store a database connection string in the application's Web.Config file. Web.Config is a part of every application and provides a nice, central location for storing information that may be needed in many of your web pages. This first bit of code is just an .aspx file. The code for accessing the connection string is located in the associated .vb code-behind file shown immediately after the .aspx code.
' Program: ConnString.aspx
' By: John Kilgo
' Date: November 28, 2002
' CodeBehind: ConnString.aspx.vb
' Purpose: Demonstrates using Web.Config to hold a connection string
' and reading that string from a program

<%@ Page Inherits="ConnString" Src="ConnString.aspx.vb" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="">
<form id="Form1" runat="server">
<asp:DataGrid id="dataGrid"
    <asp:BoundColumn HeaderText="Company Name" DataField="CompanyName" />
    <asp:BoundColumn HeaderText="Contact Name" DataField="ContactName" />
    <asp:BoundColumn HeaderText="Contact Title" DataField="ContactTitle" />
Next is the code-behind page which accesses the Web.Config file to obtain the database connection string. Notice that we must Import the namespace System.Configuration in order to access the Web.Config file.

Web.Config (shown after the .vb code) contains a key-value pair which specifies a key of "ConnectionString" with a value which is the connection string. We set a string variable ("strConnection") equal to ConfigurationSettings.AppSettings("ConnectionString"). We have now captured the connection string for use in our program.

Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Public Class ConnString : Inherits Page

  Protected dataGrid As DataGrid

  Protected Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)
    Dim sqlConn As SqlConnection
    Dim sqlCmd As SqlCommand
    Dim strConnection As String

      'Get connection string from Web.Config
      strConnection = ConfigurationSettings.AppSettings("ConnectionString")

      sqlConn = New SqlConnection(strConnection)
      sqlCmd = New SqlCommand("SELECT * FROM Customers WHERE " _
              & "(CompanyName LIKE 'A%') OR (CompanyName LIKE 'B%')", sqlConn)
      dataGrid.DataSource = sqlCmd.ExecuteReader()
    Catch ex As Exception
      Response.Write(ex.ToString & "<br>")
    End Try
  End Sub

End Class
The Web.Config file is shown below. What we have added to enable storing of the connection string is shown in red.
<?xml version="1.0" encoding="utf-8" ?>
<!-- Web.Config Configuration File -->
    <add key="ConnectionString"
      value="server=localhost;database=Northwind;uid=sa;password=secret;" />

    <customErrors mode="Off"/>
Conclusion: In this article you have seen how to store a database connection string in Web.Config and to access the connection string from a web page. You may run this program by clicking here. You may download the code by clicking Here.


配置文件(片段) jdbc:mysql://localhost:3306/contact?useSSL=false;serverTimezone=UTC异常java.sql.SQLNonTransie...
  • KooKing_L
  • KooKing_L
  • 2017年09月05日 22:22
  • 305

Store Images in Your Database

 February 2001Getting StartedStore Images in Your Database Create an easy way to save and retrieve i...
  • sonicdater
  • sonicdater
  • 2001年12月07日 09:17
  • 627

Store Connection String in Web.config

Connection string in .NET 2.0 config file In the appSettings location, add a key named whatever you...
  • lk5423968
  • lk5423968
  • 2013年10月29日 14:45
  • 665 Database connection could not be established. Either the database is down or the APPS c

EBS R12.X 如题 Database connection could not be established. Either the database is down ...
  • x_focus
  • x_focus
  • 2013年12月25日 15:31
  • 2176


137. Your database instance is currently configured to support 1,500 connections. The Web applicatio...
  • rlhua
  • rlhua
  • 2013年10月26日 18:14
  • 6776

Validating Receipts With the App Store

  • JK0803_wantao
  • JK0803_wantao
  • 2015年02月09日 11:23
  • 1844


安装过程可以参考: 安装部署OSQA 用 django south...
  • huyoo
  • huyoo
  • 2011年06月05日 00:54
  • 2376


43. In your database instance, the STATISTICS_LEVEL initialization parameter is set to BASIC. What i...
  • rlhua
  • rlhua
  • 2013年10月15日 21:15
  • 9796

Android Studio:Connection failed. Please check your network connection and try again

提示信息:Connection failed. Please check your network connection and try again 找到Android Studio 安装目录下的...
  • ultrapro
  • ultrapro
  • 2015年02月23日 16:11
  • 1950

MySQL workbench 报错

今天安装数据库,使用cmd连接mysql无任何问题,但是使用mysql连接是报错: [Window Title] MySQL Workbench [Main Instruction] Cannot...
  • Luan_Jia
  • Luan_Jia
  • 2016年08月16日 16:23
  • 2058
您举报文章:Store Your Database Connection String in Web.Config...