To save a database connection string in Config.web

Source: Internet
Author: User
There are several ways to save the database connection string in ASP, Asp+ provides another new way: Config.web. Many demos in QuickStart directly write the connection string in the program. This is no problem for demo purposes, but it's not going to work in practice.

This article demonstrates how to use Config.web to store connection strings. In every page asp.net you only need
Pull it up and you can use it directly. The advantage of this is safe, the second is convenient, change the password only need to change a place.

Cut the crap, here's the code: (Put it in the root directory of the application)

Config.web
<configuration>
<appsettings>
<add key= "myconn" value= "Server=localhost;uid=sa;pwd=mypassword;database=somedatabase"/>
</appsettings>
</configuration>


Somepage.aspx
<%@ Import namespace= "System.Data"%>
<%@ Import namespace= "System.Data.SQL"%>

<script language= "VB" runat= "Server" >

Sub Page_Load (Src as Object, E as EventArgs)

' This is the meat of calling the DSN out of the Config.web

' Setting a local variable to hold the connection string variable
Dim MyConnection as SqlConnection
Dim Config as HashTable

' Setting a ' variable to hold the connection string
Config = Context.getconfig ("appsettings")
MyConnection = New SqlConnection (Config ("MyConn"))

' Setting a Command object to insert some data into a database
Dim MyCommand as SQLCommand

Dim Parm1 As String = "Sometextvalue"
Dim parm2 As String = "SomeTextValue2"

Dim insertcmd as String = "Insert into tablename values (@parm1, @parm2)"

' Using the connection string
mycommand = New SQLCommand (insertcmd, MyConnection)

MYCOMMAND.PARAMETERS.ADD (New SqlParameter ("@Parm1", Sqldatatype.varchar, 50))
Mycommand.parameters ("@Parm1"). Value = Parm1

MYCOMMAND.PARAMETERS.ADD (New SqlParameter ("@Parm2", Sqldatatype.varchar, 50))
Mycommand.parameters ("@Parm2"). Value = Parm2

MyCommand.ActiveConnection.Open ()
Mycommand.execute ()
MyCommand.ActiveConnection.Close ()

End Sub
</script>




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.