ASP.NET initial

zhaozj2021-02-16  164

ASP.NET initial

Author: Fantasia time: 2003-11-9 Document Type: Original from: Blue ideal view statistics year: 4632 | Quarter: 854 | Month: 185 | Week: 11 | today: 11

This article does not expell how to display data in the database, only propose how to increase, delete, modify data, and put forward in subsequent articles on how to display data. 1. Define OLEDBCOMMAND Type Variables: MyCommand To increase, delete, modify the database. We also need to define an OLEDBCommand or SQLCommand object according to the type of MyConnectio (please note that if MyConnection is the OLEDBConnection type, then you can only use oledbcommand; if MyConnection is SqlConnection type, then you can only use sqlcommand. It is assumed that MyConnection is an OLEDBConnection class). Method 1 You can drag and drop an OLEDBCommand like drag and drop myConnection and name MyCommand. In the second method (associated files) .CS file protected System.Data.OleDb.OleDbConnection MyConnection; manually add the following: protected System.Data.OleDb.OleDbCommand MyCommand; the private void InitializeComponent () in this.MyConnection = newSystem.Data. OLEDB.OLDBCONNECTION (); next to the way: this.mycommand = new system.data.oledb.oledbcommand (); you can complete the definition of MyCommand Description: MyCommand's role is used to execute SQL commands II, use definition MyConnectio and MyCommand increase, delete, and modify the database first we need to connect and open a database (for the database's connection and open operations to see our previous article). Open the database: MyConnectio.Open (); then we need to specify the SQL command to be executed to myCommand: mycommand.commandtext = "delete from admin"; then we need to specify a data source to myCommand (execute SQL commands to that database): MyCommand. Connection = myconnection; then we execute myCommand command: mycommand. ExecutenononQuery (); if we do "delete from admin"; need to be executed after "Insert Intmin_code, admin_pwd) Values ​​('aa', ' BB '), then we only specify myCommand again specify the SQL command to be executed: myCommand.commandtext = "Insert Intmin (Admin_code, Admin_PWD) VALUES (' AA ',' BB ')", then execute MyCommand. ExecutenonQuery () .

(Because the database is not closed, we don't need to be myconnectio.Open (); In Page_Load (), the addition, delete, modification of the database, and finally we will summarize the usage of ExecuteNonQuery (), ExecuteScalar (), ExecuteReader 1, add new record private void Page_load (Object Sender, System.EventArgs E) {MyConnection .Open (); 'Open Database MyCommand1.commandtext = "Insert Into Admin Values ​​(' Aaddq ',' AS ',' SS '"; MyCommand1.connection = MyConnection; mycommand1.executenonQuery ();' Due to add a record So returning 1 // or mycommand1.executeReader (); first add a record, then return a system.data.oledb.oledBDataReader type object, the object is: eof // or mycommand1. ExecuteScalar (); first add a record first Returns unpreamed object myconnection.close ();} 2, delete existing data private void page_load (object sender, system.eventargs e) {MyConnection.Open (); 'Open Database MyCommand1.comMandtext = "delete * From admin "; mycommand1.connection = myconnection; mycommand1.executenononQuery (); 'Due to deleting N records, return n // or mycommand1.executeReader (); first delete N records, then return a system.Data.OleDb The object of the .oledbDataReader type, the object is: eof // or mycommand1. ExecuteScalar (); first delete N records, return unprotected objects MyConnection.c Lose ();} 3, modify the existing data private void page_load (object sender, system.eventargs e) {MyConnection.Open (); 'Open Database MyCommand1.comMandText = "Update Admin SET Admin_code =' 212 ', admin_pwd =' 43 'WHERE admin_code = '23' "; mycommand1.connection = myConnection; mycommand1.executenonury (); 'Due to modifying 1 record, returning n // or mycommand1.executeReader (); first modify 1 record, then Returns an object of a system.data.oledb.oledBDataReader type, which is: eof // or mycommand1. ExecuteScalar (); first modify 1 record, return unprotected object myconnection.close ();} About MyCummand's ExecuteNonQuery (), ExecuteScalar (), ExecuteReader method: 1. ExecutenonQuery (): Perform SQL, return a integer variable,

If SQL is a record of the database, then the number of records affects the operation affects, if it is sql = "CREATE TABLE LOOKUPCODES (Code_id Smallint Identity (1, 1) Primary Key Clustered, Code_Desc Varchar (50) Not null" So This method returns -1 after the table creation is successful. For example: private void Page_Load (object sender, System.EventArgs e) {MyConnection.Open (); 'Open Database MyCommand1.CommandText = "CREATE TABLE LookupCodes (code_id smallint IDENTITY (1,1) PRIMARY KEY CLUSTERED, code_desc varchar (50) NOT NULL) "; myCommand1.connection = myConnection; myCommand1.executenonQuery (); first build a lookupCodes table, then return -1 // or mycommand1.executeReader (); first build a lookupcodes table, then return a system.data. OLEDB.OLEDBDATAREADER type object, this object is: eof // or mycommand1. ExecuteScalar (); first build a lookupCodes table, return unprotected object myconnection.close ();} 2, executescalar (): Execute SQL, (If SQL is Query SELECT) Returns the first line of the query results, if (if SQL is not querying SELECT), returning unprinted objects, because the object is not substantial, so the return result cannot be TOSTRING (), Can't equals (null), that is, if the return result does not have any effect 3, the ExecuteReader method performs SQL, (if SQL is querying SELECT) Returns the collection of query results, the type is system.data.oledb.oledBDataReader, you can pass this result, Get the data of the query. If (if SQL is not a query select), return a collection (EOF) four of the system.data.oledb.oledbdataReader type without any data, summary: ASP.NET is much more operational for the database, you must implement unified targets People may take different ways, as if there are people in ASP like to use RS.Addnew, some people like to use "Insert Into", mainly to see personal habits, of course, different methods in performance may exist Big difference, this can only rely on our accumulation of experience in the usual study.

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

New Post(0)