如何设置表适配器的连接字符串(为动态设置的字符串) [英] How to set a table adapter's connection string (to one that is dynamically set)

查看:165
本文介绍了如何设置表适配器的连接字符串(为动态设置的字符串)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是从另一个不再在公司工作的开发人员那里继承了数据库专家的角色,所以如果我听起来特别讨厌,请原谅我。

I just inherited the role of "Database Guy" from another developer who is no longer with the company, so please forgive me if I sound particularly noobish.

The该应用程序是VB.NET 4应用程序。

The application is a VB.NET 4 application.

我们的表适配器的数据库连接字符串是在my.settings(My.Settings.DBConnectionString)中设置的字符串。在运行时。当我不得不修改表适配器时,它们在My.Settings.DBConnectionString中看不到任何数据,因此在设置真实(或硬编码)连接字符串之前不允许我编辑它们。现在,我想将其更改回动态设置的位置,但是Visual Studio似乎不想让我这样做。我相信我发现在.xsd文件后面的自动生成代码中可以更改特定表适配器的连接字符串,但是如果这样做,会发生不好的事情吗?还是除了我不知道的Visual Studio侧面的属性窗格以外,还有其他机制可以更改表适配器的连接字符串?

The DB connection string for our table adapters was a string in my.settings (My.Settings.DBConnectionString) that is being set at runtime. When I had to modify the table adapters they couldn't see any data in My.Settings.DBConnectionString and thus did not allow me to edit them until I set a "real" (or hard-coded) connection string. Now I want to change it back to the dynamically set one, but Visual Studio doesn't seem to want to let me do that. I believe I've found the spot in the auto-generated code behind the .xsd file to be able to change the connection string for a particular table adapter, but if I do that will bad things happen? Or is there some other mechanism for changing a table adapter's connection string other than the properties pane on the side of Visual Studio that I am not aware of?

只是作为辅助设备问题,这里是否存在不良/非最佳做法?

Just as a secondary question, are there bad / not-best practices going on here?

谢谢!

推荐答案

这是我解决运行时更改连接字符串的问题的方法。希望对您有所帮助。

Here is how I solved the problem of changing the connection string at runtime. Hope this helps.

在我的设置中,我有2个条目

In my settings, I have 2 entries

我有一个名为DataSet1的数据集

I have a Dataset called DataSet1

我有3种表单,分别称为Form1,Form2和Form3

I have 3 forms called Form1, Form2 and Form3

Form1具有以下控件

Form1 has the following controls

以及以下代码

Public Class Form1

    Private Sub GenericoBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles GenericoBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.GenericoBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.DataSet1)

    End Sub



    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Me.TableAdapterManager.Connection.ConnectionString = My.MySettings.Default._001NewConnectionString

        Me.TableAdapterManager.UpdateAll(Me.DataSet1)

        Label5.Text = My.MySettings.Default._001NewConnectionString

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        My.MySettings.Default("_001NewConnectionString") = "Data Source=" & TextBox1.Text & "\" &
            TextBox2.Text & ";Initial Catalog=001;Persist Security Info=True;User ID=" &
            TextBox3.Text & ";Password=" & TextBox4.Text
        My.MySettings.Default.Save()

        Me.TableAdapterManager.Connection.ConnectionString = My.MySettings.Default._001NewConnectionString
        Me.TableAdapterManager.UpdateAll(Me.DataSet1)

        Try
            Me.GenericoTableAdapter.Fill(Me.DataSet1.generico)
        Catch ex As Exception
            MessageBox.Show("error Form1")
        End Try
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim f As New Form2(TableAdapterManager.Connection.ConnectionString)
        f.ShowDialog()

    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim f As New Form3(TableAdapterManager.Connection.ConnectionString)
        f.ShowDialog()
    End Sub
End Class

Form2具有以下控件

Form2 has the following controls

,并带有以下代码

Public Class Form2

    Private Sub UtentesBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles UtentesBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.UtentesBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.DataSet1)

    End Sub
    Public Sub New(ConnectionString As String)
        InitializeComponent()
        Me.TableAdapterManager.Connection.ConnectionString = ConnectionString
        Me.TableAdapterManager.UpdateAll(Me.DataSet1)
    End Sub




    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Try
            Me.UtentesTableAdapter.Fill(Me.DataSet1.utentes)
        Catch ex As Exception
            MessageBox.Show("Form2")
        End Try



    End Sub
End Class

和Form3具有以下控件

and Form3 has the following controls

,并输入以下代码

Public Class Form3
    Public Sub New(ConnectionString As String)
        InitializeComponent()
        Me.TableAdapterManager.Connection.ConnectionString = ConnectionString
        Me.TableAdapterManager.UpdateAll(Me.DataSet1)
    End Sub
    Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            Me.UtentesTableAdapter.Fill(Me.DataSet1.utentes)
        Catch ex As Exception
            MessageBox.Show("Form3")
        End Try


    End Sub

    Private Sub UtentesBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles UtentesBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.UtentesBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.DataSet1)

    End Sub
End Class

这篇关于如何设置表适配器的连接字符串(为动态设置的字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆