Introduction This document explains how to configure a connection string at runtime for a Windows application using C#. This will help the user to switch connections during run time. Also if there are multiple servers and. Hi,I m using ASP.NET C# application for.Net 2008. My question is that in my web.Config page,I want to set timeout propety for ConnectionString. I m using connection string like that <connectionStrings> <remove name. Programmatically Add or Update Connection String in ASP. Net Web. Config File. In this article I will explain how to programmatically add or update connection string in ASP. Net Web. Config file using C# and VB. Net. Namespaces. You will need to inherit the following namespaces. C#using System. Xml; using System. Data. Sql. Client; VB. Net. Imports System. Xml. Imports System. Data. Sql. Client. Code snippet to add or update Connection String in Web. Config file. The following method adds or updates the connection string based on the connection string name. If the connection string with the name does not exists a new connection string node will be created in the Web. Config file. C#privatevoid Add. Update. Connection. String(string name){ bool is. New = false; string path = Server. Map. Path("~/Web. Config"); Xml. Document doc = new. In this article I will explain how to programmatically add or update connection string in ASP.Net Web.Config file using C# and VB.Net. ASP.Net SqlDataSource: Read (Set) ConnectionString from Web.Config file Here Mudassar Ahmed Khan has explained with an example, how to read (set) connection string from Web.Config file when using the ASP.Net SqlDataSource. This example reads a connection string from a Web.config file. The connectionStrings element is a ConnectionStringSettingsCollection collection of ConnectionStringSettings objects. Working with collection elements can be. Join the Stack Overflow community to: Ask programming questions Answer and help your peers Get recognized for your expertise. Xml. Document(); doc. Load(path); Xml. Node. List list = doc. Document. Element. Select. Nodes(string. Format("connection. Strings/add[@name='{0}']", name)); Xml. Node node; is. New = list. Count == 0; if (is. New) { node = doc. Create. Node(Xml. Node. Type. Element, "add", null); Xml. Attribute attribute = doc. Create. Attribute("name"); attribute. Value = name; node. Attributes. Append(attribute); attribute = doc. Create. Attribute("connection. String"); attribute. Value = ""; node. Attributes. Append(attribute); attribute = doc. Create. Attribute("provider. Name"); attribute. Value = "System. Data. Sql. Client"; node. Attributes. Append(attribute); } else { node = list[0]; } string con. String = node. Attributes["connection. String"]. Value; Sql. Connection. String. Builder con. String. Builder = new. Sql. Connection. String. Builder(con. String); con. String. Builder. Initial. Catalog = "Test. DB"; con. String. Builder. Data. Source = "myserver"; con. String. Builder. Integrated. Security = false; con. String. Builder. User. ID = "test"; con. String. Builder. Password = "1. Attributes["connection. String"]. Value = con. String. Builder. Connection. String; if (is. New) { doc. Document. Element. Select. Nodes("connection. Strings")[0]. Append. Child(node); } doc. Save(path); }VB. Net. Private. Sub Add. Update. Connection. String(name As. String) Dim is. New As. Boolean = False Dim path As. String = Server. Map. Path("~/Web. Config") Dim doc As. New. Xml. Document() doc. Load(path) Dim list As. Xml. Node. List = doc. Document. Element. Select. Nodes(String. Format("connection. Strings/add[@name='{0}']", name)) Dim node As. Xml. Node is. New = list. Count = 0 If is. New Then node = doc. Create. Node(Xml. Node. Type. Element, "add", Nothing) Dim attribute As. Xml. Attribute = doc. Create. Attribute("name") attribute. Value = name node. Attributes. Append(attribute) attribute = doc. Create. Attribute("connection. String") attribute. Value = "" node. Attributes. Append(attribute) attribute = doc. Create. Attribute("provider. Name") attribute. Value = "System. Data. Sql. Client" node. Attributes. Append(attribute) Else node = list(0) End. If Dim con. String As. String = node. Attributes("connection. String"). Value Dim con. String. Builder As. New. Sql. Connection. String. Builder(con. String) con. String. Builder. Initial. Catalog = "Test. DB" con. String. Builder. Data. Source = "myserver" con. String. Builder. Integrated. Security = False con. String. Builder. User. ID = "test" con. String. Builder. Password = "1. Attributes("connection. String"). Value = con. String. Builder. Connection. String If is. New Then doc. Document. Element. Select. Nodes("connection. Strings")(0). Append. Child(node) End. If doc. Save(path)End. Sub. Downloads. You can download the complete source code in VB. Net and C# using the download link provided below. Read connection string from web. Everybody seems to be suggesting that adding using System. Configuration. which is true. But might I suggest that you think about installing Re. Sharper's Visual Studio extension? With it installed, instead of seeing an error that a class isn't defined, you'll see a prompt that tells you which assembly it is in, asking you if you want it to add the needed using statement.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
October 2016
Categories |