Connect to access, SQL Server, and Oracle databases through vs2005.
First, create a new project and add a class for database connection.
Here we name this class database. cs. below is the content of this class.
Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. Data. oledb; // This line is manually added.
/// <Summary>
/// Summary of the database
/// </Summary>
Public class database
{
// Define a static oledbconnection object
Private Static oledbconnection con = NULL;
// Define a static string
Private Static string constr = NULL;
/// <Summary>
/// Obtain the con connection. The constr is obtained by default.
/// </Summary>
Public static oledbconnection con
{
Get
{
Getcon ();
Return con;
}
}
/// <Summary>
/// Obtain the con connection. The constr is obtained by default.
/// </Summary>
Private Static void getcon ()
{
// Obtain the database connection string from the Web. config file. The content of the web. config file is provided later.
// Note that "constr" in connectionstrings ["constr"] must be
// Consistent with the name of the connection string in Web. config, which will be discussed later
Constr = configurationmanager. connectionstrings ["constr"]. tostring ();
// Create a con object if it is null
If (con = NULL)
{
Con = new oledbconnection (constr );
}
// If the con status is not open, open it
If (con. State! = Connectionstate. open)
{
Con. open ();
}
}
/// <Summary>
/// Close the database connection
/// </Summary>
Public static void close ()
{
// If con is not empty, determine whether the con status is closed. If not, disable it.
If (con! = NULL)
{
If (con. State! = Connectionstate. Closed)
{
Con. Close ();
}
Con. Dispose ();
Con = NULL;
}
}
}
Web. config Configuration
At this point, our database connection class is basically finished, which is relatively simple. Here we only use database connection. Below I will use web. the configuration content of the config file is listed. web. the configuration of the config file is also very simple. You only need to configure the <connectionstrings/> node and change it to the following:
<Connectionstrings>
<Add name = "constr" connectionstring = "provider = Microsoft. jet. oledb.4.0; Data Source = E:/newtest/projectcheck/database. MDB; persist Security info = true "/>
</Connectionstrings>
Note that the name = "constr" and "constr" must be consistent with the connectionstrings ["constr"] mentioned above, but you can define the name at will, that is to say, if name = "AAA", it must be followed by connectionstrings ["AAA"]. Name = "AABB", which must be followed by connectionstrings ["AABB"].
Name is finished. connectionstring = "... ", the red string above is the connection string of our database. Here I use the ACCESS database connection string, enter the database connection string you want to connect to in the middle of "" to connect to the database you want to connect. Okay. Let's continue, huh? Wait. You don't know how to write your connection string? Well, I will teach you a trick.
Connection string
Right-click on the desktop to create a text file. Then we select the file and press F2. Next, double-click the file, and click the provider. Then, there are many database connections, right? Select the database you want to connect.
If you are connected to access like me, select Microsoft jet4.0 ole db Provider,
If you are connecting to SQL Server, select Microsoft ole db provider for SQL Server,
If you connect to Oracle, select Microsoft ole db provider for Oracle,
Next, enter your database path, database login name and password, and click test connection, test passed, OK. Now note that you must tick the allow password to be saved, otherwise, you will not be able to find the password when you are connected, prompting you That the username and password are incorrect, and then confirming. Use NotePad to open AAA. udl file, see, the connection string is out, and the last line is easy. Let's copy the last line (that is, the line starting with provider) to the Web. connectionstring = "" two "in the config file. OK ~
Call Method
Because we use static methods, we only need to use the class name when calling them. con is the database. con will be able to get the database connection, using the database. close () can close the connection. In fact, we can try not to close this connection because we use static objects and all database operations use this connection, which will not cause a waste of server resources, if database operations are frequent, the advantages of this method are better reflected.