国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

在config.web中保存數據庫連接串

2019-11-18 20:49:49
字體:
來源:轉載
供稿:網友
asp中有多種方法保存數據庫連接串,asp+提供了另一種新方式:config.web。quickstart中的許多demo都是直接將連接串寫在程序中。這對于demo用途是沒有問題,但在實際使用中是不行的。

本文示范如何使用config.web來存儲連接串。在每頁asp.net中你只需用
調出來就可以直接使用了。這樣做的好處一是安全,二是方便,改密碼時只需改一個地方即可。

廢話少說,這里就是code:(放在該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 local 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>





發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 明水县| 金山区| 手机| 施秉县| 新安县| 大邑县| 扎囊县| 郴州市| 金塔县| 射洪县| 汶上县| 延寿县| 黑龙江省| 搜索| 黔西| 泊头市| 九江县| 通榆县| 耿马| 油尖旺区| 白沙| 井冈山市| 嘉兴市| 淳安县| 封丘县| 新干县| 宿迁市| 河北省| 康平县| 轮台县| 古丈县| 尉犁县| 梅州市| 佛坪县| 阿坝县| 江都市| 石柱| 临洮县| 惠州市| 景谷| 沂水县|