1

ウィザードを使用してデータセットを構築し、そこに接続を追加しました。

データセットに設定されているものではなく、Web 構成で定義されている接続文字列を使用したいと考えています。

私は次のコードを持っています (私はあなたが見る必要のない多くのものを取り出しました)

部分パブリック クラス downloaditems Inherits 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
4

2 に答える 2

6

接続文字列は次のように取得できます

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

最初に DataSet デザイン ビューに移動し、テーブルを選択し、tableAdapter を右クリックして、その接続修飾子を Public に変更します (下の図を参照)。コード ビハインドでアダプタ接続プロパティにアクセスできるようになりました。

tamboFeed.Connection = constring 

アクセス修飾子の変更については、下の図を参照してください。ここに画像の説明を入力

画像参照:リンク

更新された回答: 問題は、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>
于 2012-08-17T16:20:35.853 に答える