السلام عليكم ورحمه الله وبركاته
ان شاء الله هحاول اضع كل يوم مقاله قصيرة هنا نتعلم منها الكثير من المقال وردودكم ان شاء الله ومقالة اليوم ستكون عن كيفية انشاء 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");
}ارجو ان اكون افدتكم بشي