TMB*_*B87 1 vb.net sql-server asp.net
我使用wizzard构建了一个数据集,并在那里添加了一个连接.
我现在想要使用在我的web配置中定义的连接字符串,而不是在数据集中设置的是什么.
我有以下代码(我已经采取了很多你不需要看的东西)
部分公共类downloaditems继承System.Web.UI.Page
Run Code Online (Sandbox Code Playgroud)Private dtmboFeed As dsmbo.mboFeedDataTable Private tamboFeed As New dsmboTableAdapters.mboFeedTableAdapter Private itemCount As Integer = 0 Private changedItem As Boolean = False Private headSource As String Private footSource As String Private sideSource As String Private lastHead As String Private lastFoot As String Private lastSide As String Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load feedChecks() If changedItem = True Then If itemCount = "3" Then savetodatabase(headSource, footSource, sideSource) End If End If End Sub Private Sub checkSite(ByVal URL As String, ByVal Type As String) Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create(URL) request.UserAgent = ".NET Framework Test Client" Dim response As System.Net.HttpWebResponse = request.GetResponse() Dim sr As System.IO.StreamReader = New System.IO.StreamReader(response.GetResponseStream()) Dim sourcecode As String = sr.ReadToEnd() Dim compareHead As Integer Dim compareFoot As Integer Dim compareSide As Integer Select Case Type Case "headSource" headSource = sourcecode compareHead = String.Compare(headSource, lastHead) Case "footSource" footSource = sourcecode compareFoot = String.Compare(footSource, lastFoot) Case "sideSource" sideSource = sourcecode compareSide = String.Compare(sideSource, lastSide) End Select If Not compareHead = "0" Then changedItem = True End If If Not compareFoot = "0" Then changedItem = True End If If Not compareSide = "0" Then changedItem = True End If itemCount = itemCount + 1 End Sub Private Sub feedChecks() Dim lastImport As DateTime dtmboFeed = New dsmbo.mboFeedDataTable dtmboFeed = tamboFeed.GetCode() For Each rFeed As dsmbo.mboFeedRow In dtmboFeed lastImport = rFeed.LastImport lastHead = rFeed.HeaderCode lastFoot = rFeed.FooterCode lastSide = rFeed.SideCode Next If lastImport > System.DateTime.Now.AddDays(1) Then checkSite("http://www.xxx.me/sss/header.html", "headSource") checkSite("http://www.xxx.me/sss/footer.html", "footSource") checkSite("http://www.xxx.me/sss/sidenav.html", "sideSource") Else Exit Sub End If End Sub Private Sub savetodatabase(ByVal HeaderCode As String, ByVal FooterCode As String, ByVal SideCode As String) dtmboFeed = tamboFeed.GetData() Dim rFeed As dsmbo.mboFeedRow rFeed = dtmboFeed.NewmboFeedRow rFeed.HeaderCode = HeaderCode rFeed.FooterCode = FooterCode rFeed.SideCode = SideCode rFeed.LastImport = System.DateTime.Now rFeed.Verified = "True" dtmboFeed.AddmboFeedRow(rFeed) tamboFeed.Update(dtmboFeed) lblCode.Text = lblCode.Text & "All downloaded" End Sub End Class
编辑:
下面按照要求提供我的更新代码.我收到一个错误说
Error 53 Value of type 'String' cannot be converted to 'System.Data.SqlClient.SqlConnection'.
Run Code Online (Sandbox Code Playgroud)
码:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim constring As String
constring = ConfigurationManager.ConnectionStrings("ConnectString").ToString()
tamboFeed.Connection = constring
feedChecks()
If changedItem = True Then
If itemCount = "3" Then
savetodatabase(headSource, footSource, sideSource)
End If
End If
End Sub
Run Code Online (Sandbox Code Playgroud)
您可以将连接字符串作为
Dim constring as String
constring = ConfigurationManager.ConnectionStrings("YouconnectionStringNameinWebConfig").ConnectionString
Run Code Online (Sandbox Code Playgroud)
现在首先转到DataSet设计视图,选择表格,右键单击tableAdapter并将其连接修改器更改为Public(见下图),现在您可以在代码隐藏中访问适配器连接属性.
tamboFeed.Connection = constring
Run Code Online (Sandbox Code Playgroud)
请参阅下图以了解更改访问修饰符.
图片参考:链接
更新的答案: 问题是您已将连接字符串放在webconfig的AppSettings部分中,将连接字符串添加到ConnectionString部分.见下面的代码
<connectionStrings>
<add name="ConnectString" connectionString="data source=server;initial catalog=database;persist security info=False;user id=adsadasda;password=asdsadasd;packet size=4096" />
</connectionStrings>
Run Code Online (Sandbox Code Playgroud)