网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > ASP技巧
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.asp中vbscript访问xml文件.
.用XMLHTTP组件正确解析图片地址并.
.ASP连接数据库的全能代码.
.Display data From database int.
.ASP+SQLServer2000 经验积累.
.构建你的网站新闻自动发布系统之.
.ASP技术在论坛中的运用(八)(吐血.
.利用ASP实现事务处理的方法.
.一个分页存储过程.
.将html源代码规范化,转换成XSL代.
.ASP跳过错误.
.透过ASP修改NT使用者的密码.
.将ASP代码移植为VB COM组件-4.
.浅谈ASP中Web页面间的数据传递.
.ADO如何提供异动功能?(BIG5).
.一个简单的用存储过程分页.
.ASP中FSO对象对IIS WEB服务器数据.
.在 Access 中使用“存储过程”.
.用ASP编程控制在IIS建立Web站点的.
.改善ASP性能和外观的技巧集锦(上.

在config.web中保存数据库连接串

发表日期:2001-5-10


在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>




上一篇:带日期标注的日历控件 人气:13574
下一篇:动态创建下拉菜单 人气:15320
浏览全部的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐