This articleArticleFor the moment, I will not mention how to display the data in the database, but how to add, delete, or modify the data. I will discuss how to display the data in future articles.
1. Define oledbcommand type variable: mycommand
To add, delete, and modify a database, we also need to define an oledbcommand or sqlcommand object based on the myconnection type (Note that if myconnection is oledbconnection type, you can only useOledbcommandIf myconnection is of the sqlconnection type, you can only useSqlcommand. Assume that myconnection is an oledbconnection class ).
Method 1
You can drag and drop an oledbcommand like dragging and dropping myconnection and name it mycommand.
Method 2
In the (associated file). CS File protected system. Data. oledb. oledbconnection myconnection; Add the following manually:
Protected system. Data. oledb. oledbcommand mycommand;
In private void initializecomponent (), manually add the following line of this. myconnection = Newsystem. Data. oledb. oledbconnection:
This. mycommand = new system. Data. oledb. oledbcommand ();
You can complete the definition of mycommand.
Note:Mycommand is used to execute SQL commands.
2. Use the defined myconnection and mycommand to add, delete, and modify databases.
First, we need to connect to and open a database (for database connection and open operations, please refer to our previous articles ).
Open Database: myconnectio. open ();
Then, we need to specify the SQL command to be executed for mycommand:
Mycommand. commandtext = "delete from admin ";
Next we need to specify the data source for mycommand (execute the SQL command for that database): mycommand. Connection = myconnection;
Then we can execute the mycommand command: mycommand. executenonquery ();
If we still execute "delete from admin ";
Then run insert into admin (admin_code, admin_pwd) values ('A', 'bb ')",You only need to specify mycommand again to specify the SQL command to be executed: Mycommand. commandtext = "insert into admin (admin_code, admin_pwd) values ('A', 'bb')", and then execute mycommand. executenonquery. (Because the database is not closed, we do not need to or can't myconnectio again. open (); similarly, because the data source of mycommand is not changed, we do not need to specify mycommand again. connection = myconnection ;)
Next we will explain in detail how to add, delete, and modify databases in page_load (). Finally, we will summarize the usage of executenonquery (), executescalar (), and executereader.
1. Add new records
Private void page_load (Object sender, system. eventargs E)
{
Myconnection. open (); 'Open the database
Mycommand1.commandtext = "insert into admin values ('addq', 'as', 'ss ')";
Mycommand1.connection = myconnection;
Mycommand1.executenonquery (); 'returns 1 because a record is added.
// Or mycommand1.executereader (); First add a record, and then return an object of the system. Data. oledb. oledbdatareader type, which is EOF
// Or mycommand1. executescalar (); first, add a record to return objects that are not actually listed.
Myconnection. Close ();
}
2. Delete existing data
Private void page_load (Object sender, system. eventargs E)
{
Myconnection. open (); 'Open the database
Mycommand1.commandtext = "delete * from admin ";
Mycommand1.connection = myconnection;
Mycommand1.executenonquery (); 'Because N records are deleted, n is returned.
// Or mycommand1.executereader (); Delete N records first, and then return an object of the system. Data. oledb. oledbdatareader type. The object is EOF
// Or mycommand1. executescalar (); Delete N records first and return objects not listed in real time.
Myconnection. Close ();
}
3. Modify existing data
Private void page_load (Object sender, system. eventargs E)
{
Myconnection. open (); 'Open the database
Mycommand1.commandtext = "Update admin set admin_code = '000000', admin_pwd = '43 'Where admin_code = '23 '";
Mycommand1.connection = myconnection;
Mycommand1.executenonquery (); 'returns N because one record is modified.
// Or mycommand1.executereader (); First modify one record, and then return an object of the system. Data. oledb. oledbdatareader type, which is EOF
// Or mycommand1. executescalar (); first, 1 record is modified to return objects not listed.
Myconnection. Close ();
}
3. Differences in mycommand executenonquery (), executescalar (), and executereader Methods:
1. executenonquery (): Execute SQL, returns an integer variable. If SQL is used to operate on database records, the number of records affected by the operation is returned. If SQL = "CREATE TABLE lookupcodes (code_id smallint identity) primary Key clustered, code_desc varchar (50) not null) "returns-1 after the table is created successfully. (If a table is created,-1 is returned.)
example:
private void page_load (Object sender, system. eventargs e)
{< br> myconnection. open (); 'Open the database
mycommand1.commandtext = "create table lookupcodes (code_id smallint identity () primary key clustered, code_desc varchar (50) not null )"; mycommand1.connection = myconnection;
mycommand1.executenonquery (); 'first create a lookupcodes table, and then return-1
// or mycommand1.executereader (); first create a lookupcodes table, then return a system. data. oledb. oledbdatareader type object, which is EOF
// or mycommand1. executescalar (); first, create a lookupcodes table and return the objects not actually listed
myconnection. close ();
}
2. executescalar (): executes the SQL statement (if the SQL statement is to query the SELECT statement)Returns the first column of the first row of the query result.If (If SQL is not a query select)Returns objects that are not actually columned. Because objects are not actually columned, the returned results cannot be tostring () or equals (null). That is to say, the returned results have no effect.
3. Execute the SQL statement using the executereader method (if the SQL statement is query select)Returns a set of query results.The data type is system. Data. oledb. oledbdatareader. You can use this result to obtain the queried data. If (If SQL is not a query select), a set of system. Data. oledb. oledbdatareader types (EOF) without any data is returned)
Iv. Summary:
ASP. net has many database operation methods. To achieve a unified goal, different people may adopt different methods, as if some people in ASP prefer to use Rs. addnew, some people like to use "insert into". It mainly depends on their habits. Of course, there may be great differences in performance between different methods, this can only rely on the accumulation of 1.1 pieces of experience in our ordinary learning. In addition, the ASP. NET page provides methods similar to the following:
Oledbcommand2.parameters ("au_id"). value = textbox1.text
Oledbcommand2.parameters ("au_lname"). value = textbox2.text
Oledbcommand2.parameters ("au_fname"). value = textbox3.text
Oledbcommand2.parameters ("phone"). value = textbox4.text
Oledbcommand2.parameters ("Address"). value = textbox5.text
Oledbcommand2.parameters ("city"). value = textbox6.text
Oledbcommand2.parameters ("St"). value = textbox7.text
Oledbcommand2.parameters ("Zip"). value = textbox8.text
Oledbcommand2.parameters ("contract"). value = checkbox1.checked
Cmdresults = oledbcommand2.executenonquery ()