App.config上的2个或更多ConnectionString MySQL VB.Net

时间:2016-08-17 02:56:49

标签: mysql vb.net app-config

我有一个代码可以从我的VB.Net项目中的App.Config返回 ConnectionString 。我的问题是,我是打电话给 online1 还是 offline2 ,是否正在调用 offline2值,或者更可能是默认值

我希望在GetConnectionString([ConnectionString Name])

上获得连接字符串
Public Shared Function GetConnectionString(ByVal strConnection As String) As String
    'Declare a string to hold the connection string
    Dim sReturn As New String(" ")
    Dim connections As ConnectionStringSettingsCollection = ConfigurationManager.ConnectionStrings
    'Check to see if they provided a connection string name
    If Not String.IsNullOrEmpty(strConnection) Then
        For Each connection As ConnectionStringSettings In connections
            If connection.Name = strConnection Then
                'Retrieve the connection string fromt he app.config
                sReturn = connection.ConnectionString
            Else
                'Since they didnt provide the name of the connection string
                'just grab the default on from app.config
                sReturn = connection.ConnectionString
            End If
        Next
    End If
    Return sReturn
End Function

App.Config中

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <connectionStrings>
    <add name="online1" connectionString="SERVER=127.0.0.1; DATABASE=lto_db; UID=root; PASSWORD=;" providerName="MySql.Data.MySqlClient" />
    <add name="offline2" connectionString="SERVER=localhost; DATABASE=lto_db; UID=root; PASSWORD=;" providerName="MySql.Data.MySqlClient" />
  </connectionStrings>

</configuration>

1 个答案:

答案 0 :(得分:0)

只需使用:

myPlayer = amp("azuremediaplayer", myOptions,function(){
                    this.addEventListener(amp.eventName.loadedmetadata, function () {
                        var shareOption = new Amp.Plugin.Share.ShareOptions;
                        shareOption.socialShare.shareIcons.push(Amp.Plugin.Share.SocialShareIcon.getPredefinedShareIcon(0 /* Facebook */));
                        shareOption.socialShare.shareIcons.push(Amp.Plugin.Share.SocialShareIcon.getPredefinedShareIcon(1 /* Twitter */));
                        shareOption.socialShare.shareIcons.push(Amp.Plugin.Share.SocialShareIcon.getPredefinedShareIcon(2 /* LinkedIn */));
                        shareOption.socialShare.shareIcons.push(Amp.Plugin.Share.SocialShareIcon.getPredefinedShareIcon(3 /* Mail */));
                        this.share(shareOption);/* plugin initialization*/
                    });
                });

如果您还没有System.Configuration

,则需要对其进行引用

您的代码的问题是,如果连接名称为null或为空,您甚至不执行循环并且不返回默认值(您只返回“”)。如果他们确实指定了一个名称并且它不是第一个,那么即使有更多的连接要检查,你也会返回默认值并退出。