This article describes how to connect C # To the database through web. config configuration and call methods.
Connect to access, SQL Server, and Oracle databases through vs2005.
Next we will introduce C # connecting to the database:
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.
-
- UsingSystem;
-
- UsingSystem. Data;
-
- UsingSystem. configuration;
-
- UsingSystem. Web;
-
- UsingSystem. Web. Security;
- UsingSystem. Web. UI;
-
- UsingSystem. Web. UI. webcontrols;
-
- UsingSystem. Web. UI. webcontrols. webparts;
-
- UsingSystem. Web. UI. htmlcontrols;
-
- UsingSystem. Data. oledb;// This line is manually added.
-
- /// <Summary>
- /// Summary of the database
-
- /// </Summary>
-
- Public ClassDatabase
-
- {
-
- // Define a static oledbconnection object
-
- Private StaticOledbconnection con =Null;
- // Define a static string
-
- Private Static StringConstr =Null;
-
- /// <Summary>
-
- /// Obtain the con connection. The constr is obtained by default.
-
- /// </Summary>
- Public StaticOledbconnection con
-
- {
-
- Get
-
- {
-
- Getcon ();
-
- ReturnCon;
-
- }
-
- }
-
- /// <Summary>
-
- /// Obtain the con connection. The constr is obtained by default.
- /// </Summary>
-
- Private Static VoidGetcon ()
-
- {
-
- // 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 =NewOledbconnection (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 VoidClose ()
-
- {
-
- // 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 " /> / LI>
- /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.
C # database connection: connection string
Right-click on the desktop to create a text file. Then we select the file and press F2. Double-click the file, and then click provideProgramSee, there are a lot of 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 ~
C # connect to the database: 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.