1

我在几个 SQL 数据库中有一个日志文件。

我想在绑定到其中一个的 ASP 页面上有一个网格。

我想在页面上有几个按钮来改变填充网格视图的表格。

这是一个 ASP VB 网站项目。

任何人都可以帮忙吗?

谢谢!

4

2 回答 2

0

这似乎有效。希望此代码对其他人有所帮助。感谢所有评论的人...

Web.config 信息:

<connectionStrings>
    <add name="Data1" connectionString="Data Source=Server1;Initial Catalog=DatabaseSame;Integrated Security=True" providerName="System.Data.SqlClient"/>
    <add name="Data2" connectionString="Data Source=Server2;Initial Catalog=DatabaseSame;Integrated Security=True" providerName="System.Data.SqlClient"/>
    <add name="Data3" connectionString="Data Source=Server3;Initial Catalog=DatabaseSame;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>

Webform 有一个数据网格和多个按钮来选择哪个数据库和/或服务器

后面的代码:

Imports System.Configuration

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub SetNewConnectionForDataSource(ByVal strConnectionName As String)
    'Pull data from webconfig based on the connection name
    Dim strConnectionString As String = ConfigurationManager.ConnectionStrings(strConnectionName).ConnectionString

    Me.SqlDataSource1.ConnectionString = strConnectionString

    'Save to viewstate for postback
    ViewState("CurrentConnection") = strConnectionString

    'Let user know what connection they are viewing
    lblEnvironment.Text = strConnectionName

End Sub

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

    If IsPostBack Then
        Me.SqlDataSource1.ConnectionString = ViewState("CurrentConnection")
    Else
        'First page load, set database
        SetNewConnectionForDataSource("Data1")
    End If

End Sub

Protected Sub btnOne_Click(sender As Object, e As System.EventArgs) Handles btnDev.Click
    SetNewConnectionForDataSource("Data1")
End Sub

Protected Sub btnTwo_Click(sender As Object, e As System.EventArgs) Handles btnProd.Click
    SetNewConnectionForDataSource("Data2")
End Sub

Protected Sub btnThree_Click(sender As Object, e As System.EventArgs) Handles btnProd.Click
    SetNewConnectionForDataSource("Data3")
End Sub


End Class
于 2013-08-07T19:58:27.530 回答
0

只需让按钮通过不同的方法绑定数据。例如,按钮 A 调用方法 A,该方法使用连接字符串 A 来填充网格视图。

于 2013-08-07T14:30:24.907 回答