When I used Delphi programming, the data connection information was generally stored in INIFILE, And the password was encrypted. C # programming, I store the connection parameters in the XML file and encrypt the password. Multiple connections can be stored in an XML file.
The XML file for storing data source connection parameters is as follows:
<? XML version = "1.0" encoding = "UTF-8" standalone = "yes"?>
<Connections>
<Connection name = "kkerp" DESC = "Open Group ERP Database" type = "SQL">
<Param name = "datasource" caption = "Data Source" value = "(local)"/>
<Param name = "userid" caption = "User ID" value = "sa"/>
<Param name = "databasename" caption = "Database" value = "kkerp"/>
<Param name = "integratedsecurity" caption = "Integrated Security" value = "sspi"/>
</Connection>
<Connection name = "petshop" DESC = "Pet Store Data" type = "Ole">
<Param name = "provider" caption = "provider" value = "Microsoft. Jet. oledb.4.0"/>
<Param name = "datasource" caption = "Data Source" value = "petshop. mdb"/>
</Connection>
</Connections>
The following code reads the data connection string of the specified data connection name:
/** // <Summary>
/// Read the connection string
/// </Summary>
/// <Param name = "connname"> connection name </param>
/// <Returns> </returns>
Private string readconnstring (string connname)
...{
// Read the data connection string
String strconn = "";
String filename = @ "conn. xml ";
Filename = application. startuppath + "/" + filename;
String strxml;
Streamreader reader = new streamreader (filename );
Strxml = reader. readtoend ();
Xmldocument xmldoc = new xmldocument ();
Xmldoc. loadxml (strxml );
// Read all connections
Xmlnodelist nodelist = xmldoc. selectsinglenode ("Connections"). childnodes;
// Query the parameter set of the connection Template
For (INT I = 0; I <nodelist. Count; I ++)
...{
// Whether the connection is
Xmlelement Xe = (xmlelement) nodelist [I];
If (Xe. getattribute ("name") = connname)
...{
// Connection Parameters
Foreach (xmlnode node in Xe. childnodes)
...{
Xmlelement xe1 = (xmlelement) node;
Strconn + = xe1.getattribute ("caption") + "=" + xe1.getattribute ("value ");
}
Break;
}
}
Reader. Close ();
//
Return strconn;
}
We first read the text content of the XML file from conn. xml and store it in strxml using (streamreader) reader. Use xmldocument xmldoc to load strxml text.
In this case, you can use xmldoc to read the node name, node value, and attribute value. The connection parameter name and value are read and combined into a string.
If you have a password, use encryption/decryption.