الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

التحكم في ConnectionString من خلال الكود

بدأه C#Dev1 في 22 فبراير 2012 · 0 رد · 605 مشاهدة · في ASP.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1

السلام عليكم ورحمه الله وبركاته

ان شاء الله هحاول اضع كل يوم مقاله قصيرة هنا نتعلم منها الكثير من المقال وردودكم ان شاء الله ومقالة اليوم ستكون عن كيفية انشاء ConnectionString واعطاءه الداتا المطلوبة من خلال الكود مستخدما C#

private void AddUpdateConnectionString(string NameOfConnectionString)
    {
        // Created by Khaled Abd El Azeem Harby
        // MCTS Developer (web) &  Php Developer 
        // to Contact Ideal_Male67@Yahoo.Com
        bool isNew = false;
        // Get The Web Configuration From The Server
        string path = Server.MapPath("~/Web.Config");
        //Create Object From XmlDocument Class To Can Control The Nodes in Xml File
        XmlDocument doc = new XmlDocument();
        doc.Load(path);
        XmlNodeList list = doc.DocumentElement.SelectNodes(string.Format("connectionStrings/add[@name='{0}']", NameOfConnectionString));
        XmlNode node;
        isNew = list.Count == 0;
        if (isNew)
        {
            // now  we  will write the request of code add or remove or clear
            node = doc.CreateNode(XmlNodeType.Element, "add", null);
            XmlAttribute attribute = doc.CreateAttribute("name");
            attribute.Value = NameOfConnectionString;
            node.Attributes.Append(attribute);

            attribute = doc.CreateAttribute("connectionString");
            attribute.Value = "";
            node.Attributes.Append(attribute);

            attribute = doc.CreateAttribute("providerName");
            attribute.Value = "System.Data.SqlClient";
            node.Attributes.Append(attribute);
        }
        else
        {
            node = list[0];
        }
        string conString = node.Attributes["connectionString"].Value;

        // using SqlConnectionBuilder To Can Create And Manage The Xml File (Config)
        SqlConnectionStringBuilder conStringBuilder = new SqlConnectionStringBuilder(conString);

        // Write The Properties or Attributes Of ConnectionString 
        conStringBuilder.InitialCatalog = "TestDB";
        conStringBuilder.DataSource = "myserver";
        conStringBuilder.IntegratedSecurity = false;
        conStringBuilder.UserID = "test";
        conStringBuilder.Password = "12345";
        node.Attributes["connectionString"].Value = conStringBuilder.ConnectionString;
        if (isNew)
        {
            doc.DocumentElement.SelectNodes("connectionStrings")[0].AppendChild(node);
        }
        doc.Save(path);
    }

بالطبع سنستدعي ال Function في اي مكان في page Load or Button Event

انا استدعيت هذا الكود في ال Page_Load

protected void Page_Load(object sender, EventArgs e)
    {

        AddUpdateConnectionString("KhaledHarbyNew");
    }

ارجو ان اكون افدتكم بشي

1

Technical  Skills

  • Database
  • Microsoft SQL Server 2000/2005/2008 .
  • ADO.NET .
  • LINQ - Entity Framework
  • Oracle SQL 10G .
  • MYSQL.
  • Microsoft Access .

 

  • Web Language
  • Html5.
  • CSS3.
  • JavaScript-JQuery-Ajax
  • Web Services.
  • ASP.Net 2 , 3.5 , 4 .
  • ASP.NET MVC4
  • DevExpress 
  • Telerik
  • Php

 

  • Programming Language
  • Microsoft Visual Studio 2005 - 2008 - 2010 - 2012 
  • 4 , Microsoft C#.Net 2 , 3.5 .
  • Microsoft VB.NET.

 

 

 

Certifications

  1. University Certificate  .
  2. Deutsch A1 , A2 Certificate .
  3. Microsoft .NET Framework 2.0 - Web-based Client Development         
  4. Mastering PHP & MySQL

 

 

ERP Systems

  1. MSales
  2. Infinty

جزاكم الله كل خير

للتواصل

Email: ideal_male67@yahoo.com

Mobile : +201008265792

With My Best Wishes
Khaled Harby

-----------------------------------------------------------
اخر مواضيعي
من هنا

مواضيع مشابهة