App.cofig代碼
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="dataConfiguration"type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</configSections>
<connectionStrings>
<add name="DbDatabase" providerName="System.Data.SqlClient" connectionString=""/>
</connectionStrings>
C#代碼
public void updateConfigFile(string con)
{
//updating config file
XmlDocument XmlDoc = new XmlDocument();
//Loading the Config file
XmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
// XmlDoc.Load("App.config");
foreach (XmlElement xElement in XmlDoc.DocumentElement)
{
if (xElement.Name == "connectionStrings")
{
//setting the coonection string
xElement.FirstChild.Attributes[2].Value = con;
}
}
//writing the connection string in config file
XmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
//XmlDoc.Save("App.config");
}
private void btn_Connect_Click(object sender, EventArgs e)
{
StringBuilder Con = new StringBuilder("Data Source=");
Con.Append(txt_ServerName.Text);
Con.Append(";Initial Catalog=");
Con.Append(txt_DatabaseName.Text);
if (String.IsNullOrEmpty(txt_UserId.Text) &&String.IsNullOrEmpty(txt_Password.Text))
Con.Append(";Integrated Security=true;");
else
{
Con.Append(";User Id=");
Con.Append(txt_UserId.Text);
Con.Append(";Password=");
Con.Append(txt_Password.Text);
}
string strCon = Con.ToString();
updateConfigFile(strCon);
DatabaseTableDA da = new DatabaseTableDA();
tableList = da.Select_Tables();
this.lstTables.DataSource = tableList;
}
你通常只具有寫權限,當您運行整個應用程序作爲管理員,不是一個好理念。 – 2011-05-09 07:01:39