精华区 [关闭][返回]

当前位置:月光软件>>讨论区精华>>〖软件开发〗>>● ASP>>★ASP的延伸★>>ASP.NET>>ASP+的DB连接字符串方法 --译

主题:ASP+的DB连接字符串方法 --译
发信人: dongbao()
整理人: dongbao(2000-10-27 23:43:57), 站内信件
ASP+ Connection String method
ASP+的DB连接字符串方法    罗亭译[因E文太差,担心误导大伙,故中英对照,
请指教]

There are many ways to do connection strings in asp. ASP+ presents a n
ew frontier to perform connection strings. Many of the quickstart demo
s have the connection string listed in every page. This is ok for demo
 purposes, BUT what if you want to change your password. The only way 
is touch every page, NO WAY! 
------>
在ASP中有许多方法来定义连接字符串,ASP+提供了一种新的方法来完成连接字符
串。许多演示ASP页中分别都有定义有连接字符串,这对于演示目的来说是可行的
,但一旦你想改变你的DB密码,你唯一的方法是分别打开你的每一页来修改它,
别无它法。

This article demonstrates how to use the config.web that would store t
he connection string in one place. In each ASP+ page you would use a m
ethod to pull that value out of the config.web. This is my Favorite me
thod so far. Having your dsn configured in the config.web allows for s
ecure location and the connection string being in one place! I'm worki
ng on having the connection object in a dll but that is version two of
 the article!
------>
这篇文章示范了如何使用config.web来存贮连接字符串到一个地方[被重用,译者
注]。在每一个ASP+代码页中你需要做的仅是从config.web中取出值,这是我最喜
欢的方法。将你的DSN连接字符串配置在config.web中将是安全而且可以重用的,
我现在将我的连接对象置于一个

DLL文件中,但那将在下一篇文章中说明。

Enough talking, here is the code (Place in the root directory of the a
pplication root)
费话少说,下边是源码[请放在你的应用程序的根目录下:我想仅指的是Config.
web吧,译者注]
'--------------------------------------------------------

'关键字符串,译者注
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
这是从config.web中取出DSN字符串的最基本的方法

'Setting a local variable to hold the connection string variable
'申明一个本地变量来保存DSN连接字符串变量
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
'申明一个Command对象来往database中插入数据
Dim MyCommand As SQLCommand

dim parm1 as string = "SomeTextValue"
dim parm1 as string = "SomeTextValue2"

Dim InsertCmd As String = "Insert into tablename values (@parm1, @parm
2)"

'Using the connection string
'使用连接字符串
MyCommand = New SQLCommand(InsertCmd, MyConnection)

MyCommand.Parameters.Add(New SQLParameter("@Parm1", SQLDataType.VarCha
r, 50))
MyCommand.Parameters("@Parm1").Value = Parm1

MyCommand.Parameters.Add(New SQLParameter("@Parm2", SQLDataType.VarCha
r, 50))
MyCommand.Parameters("@Parm1").Value = Parm2

MyCommand.ActiveConnection.Open()
MyCommand.Execute()
MyCommand.ActiveConnection.Close()

End Sub
</script>

'--End

--
ICQ:43395237 OICQ:126132  
我自豪我用正版,我骄傲我用盗版!!! 

※ 来源:.月光程序代码网 http://www.moon-soft.com.[FROM: 202.108.5.176]

[关闭][返回]






转载请注明:转载自 月光程序代码网 [ http://www.moon-soft.com ]