【问题标题】:use connectionstring from webconfig rather than dataset properties使用来自网络配置的连接字符串而不是数据集属性
【发布时间】:2025-11-25 04:20:18
【问题描述】:

我使用向导构建了一个数据集并在其中添加了一个连接。

我现在想使用在我的网络配置中定义的连接字符串,而不是在数据集中设置的内容。

我有以下代码(我取出了很多你不需要看的东西)

部分公开课下载项目 继承 System.Web.UI.Page

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'.

代码:

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

【问题讨论】:

  • 请看下面的答案。它正在工作。
  • 你错过了 ToString 的函数括号。你现在有什么错误吗?
  • 错误 53 'String' 类型的值无法转换为 'System.Data.SqlClient.SqlConnection'。 (括号相同的错误)
  • 首先你提到'System.Configuration.ConnectionStringSettings'类型的错误53值,现在你提到'String'类型的错误53值不能转换为'System.Data.SqlClient.SqlConnection '` 错误从 ConnectionStringSetting 更改为 System.Data.SqlClienht.SqlConnection ?你能告诉我你的webconfig中的连接字符串吗?将 ConnectString 添加到您的代码中
  • 非常感谢您的帮助和耐心。不幸的是,我仍然收到:错误 53“字符串”类型的值无法转换为“System.Data.SqlClient.SqlConnection”

标签: asp.net sql-server vb.net


【解决方案1】:

你可以得到连接字符串为

Dim constring as String
constring = ConfigurationManager.ConnectionStrings("YouconnectionStringNameinWebConfig").ConnectionString

现在首先进入你的数据集设计视图,选择表,右键单击 tableAdapter 并将它的连接修饰符更改为公共(见下图),现在你可以在代码隐藏中访问适配器连接属性。

tamboFeed.Connection = constring 

更改访问修饰符见下图。

图片参考:Link

更新答案: 问题是您已将连接字符串放置在 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>

【讨论】:

  • 但是你怎么告诉dsmbo.mboFeedDataTable使用它呢?
  • @Andomar 我编辑了我的帖子,还添加了代码来更改数据集连接字符串。
  • 谢谢。我在哪里添加 constring = config.... 因为它在实际页面中不起作用。它是否需要在数据库 vb 上而不是我的页面代码上?
  • 将其添加到您要更改连接字符串的页面上。
  • 我收到错误 53 类型为“System.Configuration.ConnectionStringSettings”的值无法转换为“String”。
【解决方案2】:

tamboFeed.Connection 是 System.data.SqlClient.SqlConnection 类型,因此不能接受字符串赋值。只要将数据集连接修饰符设置为 Public,您就可以在一行中从 web.config 中分配连接字符串,如下所示:

tamboFeed.Connection.ConnectionString = ConfigurationManager.ConnectionStrings("YouconnectionStringNameinWebConfig").ConnectionString

【讨论】:

    最近更新 更多