使用按钮动态更改gridview的连接字符串?

时间:2013-08-07 14:20:36

标签: asp.net

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

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

我希望页面上有几个按钮可以更改哪个表填充gridview。

这是一个ASP VB网站项目。

有人可以帮忙吗?

谢谢!

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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