Database Configuration source code with a dialog box

Source: Internet
Author: User

 

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 );

}
}


}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.