学院首页>网络编程>ASP>ASP+连接字符窜的方法

ASP+连接字符窜的方法

作者:凌风翻译 来源:未知 添加时间:2006-5-21 10:29:07
首先,请将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) 

'在CONFIG.WEB外调用系统DSN

'定义一个局域变量来保存要连接的字符窜变量
Dim MyConnection As SQLConnection
Dim Config as HashTable

'定义一个局域变量来保存字符窜
Config = Context.GetConfig("appsettings")
MyConnection = New SQLConnection(Config("MyConn"))

'建立command 对象 向数据库插入数据
Dim MyCommand As SQLCommand

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

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

'开始连接
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>
站内搜索