Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using Microsoft. Data. connectionui;
Using system. xml;
Namespace dszq
{
Public partial class dataconn: Form
{
Public dataconn ()
{
Initializecomponent ();
// Filepath = environment. currentdirectory;
Filepath = application. startuppath;
Filepath + = "// app. config ";
}
String filepath;
String connstr;
/// <Summary>
///
/// </Summary>
/// <Param name = "key"> </param>
/// <Param name = "strvalue"> </param>
Public void modify (string key, string strvalue) // two parameters: the key value to be modified and the new value to be modified;
{
String flagstr = strvalue;
If (strvalue = string. Empty)
{
MessageBox. Show ("the connection string cannot be blank! ");
Return;
}
// String XPath = "/configuration/userinfo/Add [@ key = '? '] ";
Try
{
String XPath = "/configuration/appsettings/Add [@ key = '? '] ";
Xmldocument domwebconfig = new xmldocument ();
// Domwebconfig. Load (httpcontext. Current. server. mappath ("Web. config ")));
Domwebconfig. Load (filepath );
Xmlnode addkey = domwebconfig. selectsinglenode (XPath. Replace ("? ", Key )));
If (addkey = NULL)
{
// Response. write ("<SCRIPT> alert (\" not found <add key = '"+ key +" 'value =... /> Configuration section \ ") </SCRIPT> ");
MessageBox. Show ("configuration section of <add key = '" + key + "'> not found ");
Return;
}
Addkey. attributes ["value"]. innertext = strvalue;
Domwebconfig. Save (filepath );
MessageBox. Show ("database connection configuration successful", "Information prompt ");
Txtconnectionstring. Text = connstr;
Txtconnectionstring. Enabled = false;
}
Catch
{
// MessageBox. Show ("the Web. config configuration file cannot be found in the" + environment. currentdirectory + "directory", "message prompt ");
Txtconnectionstring. Enabled = true;
Txtconnectionstring. Clear ();
Txtconnectionstring. forecolor = color. Red;
Txtconnectionstring. Text = "the app. config configuration file cannot be found in the" + application. startuppath + "directory ";
Return;
}
}
Private void buttonconn_click (Object sender, eventargs E)
{
Dataconnectiondialog dialog = new dataconnectiondialog ();
// Add a data source list. You can add the data source types required by your program to the window.
Dialog. CES. Add (datasource. sqldatasource );
Dialog. CES. Add (datasource. odbcdatasource );
Dialog. selecteddatasource = datasource. sqldatasource;
Dialog. selecteddataprovider = dataprovider. sqldataprovider;
// You can only show the dialog box through the static method of the dataconnectiondialog class.
// Use dialog. Show () or dialog. showdialog () to present the dialog box.
If (dataconnectiondialog. Show (dialog, this) = dialogresult. OK)
{
Connstr = dialog. connectionstring;
Connstr = "provider = sqloledb.1;" + connstr;
Modify ("connstr", connstr );
}
}
}
}