asp.net2.0中用VB.NET如何引用在web.config中定义了连接字符串
asp.net2.0中用VB.NET如何引用在web.config中定义了连接字符串在web.config中定义的连接字符串如下:请问如何引用?如:DimconnAsNe...
asp.net2.0中用VB.NET如何引用在web.config中定义了连接字符串
在web.config中定义的连接字符串如下:
请问如何引用?
如:Dim conn As New SqlConnection(???)
---------------------------------------------------------------------------------------------------------
<add name="student" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="C:\Users\peng\Documents\Visual Studio 2008\WebSites\WebSite7\App_Data\student.mdb";Persist Security Info=True;Jet OLEDB:Database Password=198404"
providerName="System.Data.OleDb" /> 展开
在web.config中定义的连接字符串如下:
请问如何引用?
如:Dim conn As New SqlConnection(???)
---------------------------------------------------------------------------------------------------------
<add name="student" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="C:\Users\peng\Documents\Visual Studio 2008\WebSites\WebSite7\App_Data\student.mdb";Persist Security Info=True;Jet OLEDB:Database Password=198404"
providerName="System.Data.OleDb" /> 展开
2个回答
展开全部
Dim rootWebConfig As System.Configuration.Configuration
rootWebConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/MyWebSiteRoot")
Dim connString As System.Configuration.ConnectionStringSettings
If (0 < rootWebConfig.ConnectionStrings.ConnectionStrings.Count) Then
connString = rootWebConfig.ConnectionStrings.ConnectionStrings("NorthwindConnectionString")
If Not (Nothing = connString.ConnectionString) Then
Console.WriteLine("Northwind connection string = {0}", connString.ConnectionString)
Else
Console.WriteLine("No Northwind connection string")
End If
End If
rootWebConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/MyWebSiteRoot")
Dim connString As System.Configuration.ConnectionStringSettings
If (0 < rootWebConfig.ConnectionStrings.ConnectionStrings.Count) Then
connString = rootWebConfig.ConnectionStrings.ConnectionStrings("NorthwindConnectionString")
If Not (Nothing = connString.ConnectionString) Then
Console.WriteLine("Northwind connection string = {0}", connString.ConnectionString)
Else
Console.WriteLine("No Northwind connection string")
End If
End If
追问
你好,多谢回复,但无法引用WEB.CONFIG中的连接字符串,请问高手Dim conn As New SqlConnection(???)中的三个问题该填写什么内容?非常感谢
追答
Oh,Sorry,之前可能是我没注意你贴的问题,您要连接的是 OLEDB ,可我贴的是连接SQL的。不好意思。
先说下 SqlConnection的事, SqlConnection没有三个参数的构造器吧,不知你写的New SqlConnection(? ? ?)要传三个参数? SqlConnection只有SqlConnection ()和SqlConnection (String)。
这里再 贴下 连接 OLEDB(感觉你这里应该是Access数据库),其实道理都一样,.NET 提供了统一的API。
Dim connetionString As String
Dim cnn As OleDbConnection
connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=yourdatabasename.mdb;"
cnn = New OleDbConnection(connetionString)
Try
cnn.Open()
cnn.Close()
Catch ex As Exception
End Try
在之前还要Imports System.Data.OleDb;
参考资料: http://msdn.microsoft.com/zh-cn/library/ms178411(VS.80).aspx#Y0
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询