Operate the ADO database using the C # language

zhaozj2021-02-16  44

Accessing the database is part of most applications, and with the release of C # and ADO.NET, this process has become quite simple. This article will showcase the following four basic database operations: 1. Read the data. This includes an integer , Strings and date, different data types. 2. Write data. We will write these usual data types just like reading data. This is implemented through the SQL statement. 3. Update or modify the data. We still use Simple SQL statement. 4. Delete data. Use SQL. These operations are for a Microsoft Access 2000 database, but SQL or other ADO data sources can be used by simple changing the connection string. Start the first step in order to use ADO Class, we need to include Ado.Net Namespaces and Some Exquisite Date Class. Add the following lines of code you want to perform database operations. It should be placed under the name of the namespace to introduce the code line below in the class The above .using system.data; // declared variables Using system.data.ado; // Database Using System.globalization; / / Date According to the type of engineering you participate, you may need to add the SYSTEM.DATA namespace Quote. You can determine if the compiler will generate an error after you add the above code. To add the system.data namespace, you can: 1. Right-click .2.2 .2. Select "Add in the Solution Explorer-References branch. Quote "3. Select the .NET Framework tab. 4. Double-click System.Data.dll entry 5. Click OK6.System.Data Shows now appear in the reference list of Solution Explorer. Because the connection string is in most operations To use, I suggest you set it to a member of the class you want to write. Note: In your program, the path to the database file may be different .// Property Public const string db_conn_string = "driver = {Microsoft Access Driver (* .mdb)}; " " DBQ = D: ///s/testdbreadwrite//simpletest.mdb "; reading data now becomes interesting. Read data can be implemented through the AdodataReader class. (See Chris Maunder's article "ADO.NET AdodaReader class" to get more information about this class.) The steps to read data are as follows: 1. We use Adoconnection to open a database .adocon Nection conn = new adoconnection (DB_CONN_STRING); conn.open (); 2. We write a SQL statement to define the data to be taken out. The result of this data is to return an AdodataReader object. Note that the OUT key in the execute method. this In C # means by reference delivery parameters .adodataareader DR; adoCommand cmd = new adocommand ("Select * from person", conn); cmd.execute (OUT DR); 3. We loop through each record in AdodaReader until we Complete the job to do. Note: Data is directly returned as a string to the data domain name to specify the data domain .While (Dr.READ ()) {system.console.writeLine (DR ["firstname"]) } 4. We collect it, but as a good programmer we also need to add a number of try / catch / finally statements to ensure that we have handled all the errors .Try {.... database operation ...} catch (Exception EX) ) {System.console.writeline ("Reading:"); System.Console.writeline ("Error:" EX.MESSAGE); System.Console.writeline ("SQL:" SSQLCMD);

System.console.writeline ("Conn .:" DB_CONN_STRING);} Finally {// Close connection IF (Conn.State == DbObjectState.Open) Conn.close ();} Read Different Data Type DR ["stuff "] This statement is usually able to return a data. But if you return an int or DateTime object, you usually need to perform data conversion. This usually can be implemented by using the AdodaAder Many built-in converter. Also: int nordinalAgE = DR. Getordinal ("age"; int nage = Dr.GetInt32 (Nordinal); DateTime Tupdated = (DATETIME) DR ["Updated"]; Note Getordinal Location Data Use the name to read data usage. If the data domain is blank (Yes have not been filled in any data), the above code will throw an exception. To capture this situation, we can check if there is data existence with the isnull method, as follows: int nordinal = Dr.getordinal ("age") ; if (Dr.Isnull (Nordinal)) {System.console.writeline ("Age: Not Given!");} else {int Nage = Dr.Getint32 (Nordinal); System.Console.writeline ("Age:" Nage);} Insert, modify, delete, and other SQL commands, modify, and delete the process can be very easy to implement through the SQL statement. The following code executes a SQL command to insert a record. INTO PERSON (AGE, FIRSTNAME, DESCRIPTION, UPDATED " " VALUES (55, 'Bob ",' Is a Penguin ',' 2001/12/25 20:30:15 ');"; // Create a Command Object Adocommand Cmdadder = new adocommand (ssqlcommand, db_conn_string); cmdadder.activeconnection.open (); // Execute the sql commandint nnoadded = cmdadder.executenonQuery (); system.console.writeline ("/ nrow (s) added =" nnoadded "/ n"); Note: The TRY / CATCH code does not appear in the above example, However, it should include the above code. Insert the above code to insert a record by establishing a SQL command. Some matters should be noted when writing SQL commands: 1. Numerical data should be represented directly. No single quotes (') The representation of the string should be included in single quotes ('Blah'). 3. Make sure that the string does not contain any embedded single (double) quotation marks. This will make things confused. 4. Date and time data package The international general form in single quotes is expressed ('YYYY / MM / DD HH: MM: SS'). Modify the update command indicates that the data to modify and the modified action .Executenonquery () return value points to the changed record Number, so if there are 5 peter 's words below in the form, the following code will return 5.String ssqlcommand = "Update Person Set Age = 27 where firstname =' peter '"

转载请注明原文地址:https://www.9cbs.com/read-27484.html

New Post(0)