zoukankan      html  css  js  c++  java
  • Linq To Xml改变App.config中的ConnectionString的Value

        

    <connectionStrings>

        <addname="AppConfigChange.My.MySettings.Connstr"

            connectionString=

    "Data Source=wghosh2k3"sqlexpress;Initial Catalog=Northwind;Integrated Security=True"

            providerName="System.Data.SqlClient" />

    Dim sNewConnStr As String = ""

    'Get the file info

    Dim config As System.Configuration.Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)

    'Load the file info

    Dim xml = XElement.Load(config.FilePath)

    'Get the first config section (first connection string info)

    Dim connStrXML = xml.Descendants("connectionStrings").Elements().First()

    'Get the connection string value

    Dim connStr = connStrXML.Attribute("connectionString").Value

    'Create an array with ';'

    Dim arrConn() As String = connStr.Split(";")

    For i As Int16 = 0 To arrConn.Length - 1

        'Get the attribute and value splitted by "="

        Dim arrSubConn() As String = arrConn(i).Split("=")

        If (arrSubConn.Length = 2) Then

            Dim sConnAttr As String = ""

            Dim sConnValue As String = ""

            sConnAttr = arrSubConn(0)

            sConnValue = arrSubConn(1)

            'Change Database name

            If (sConnAttr = "Initial Catalog") Then

                'This is the place where you will be changing the database name

                sConnValue = "NewDBName"       

       End If

            'Generate newly altered connection string

            sNewConnStr += sConnAttr + "=" + sConnValue + ";"

        End If

    Next

    After doing everything you need to save it back to the same file,

    'Modify the existing connection string information

    connStrXML.SetAttributeValue("connectionString", sNewConnStr)

    'Saving config at the same place

    xml.Save(config.FilePath)

  • 相关阅读:
    selenium介绍
    python爬虫之requests模块介绍
    SQLAlchemy框架用法详解
    JS判断是否为移动版浏览器
    goahead Web Server 环境搭建(Linux)
    Android 应用层APP发送短信
    Git使用相关问题汇总
    Spring boot 默认首页配置
    Android Studio高版本中文输入异常
    Android ADB 常用命令详解
  • 原文地址:https://www.cnblogs.com/witer666/p/1138711.html
Copyright © 2011-2022 走看看