I. Review above
In the first article of this series, [MrYoung Tutorial: easy to learn] 1ADONET basics and login module implementation 1-1, we first learned about the concept of relational databases, then I learned how to use the Enterprise Manager to create database tables and add data, and finally learned about the basic SQL statements: Query, add, modify, and delete usage, this section describes how to implement a login form to get started with ADO. NET Programming
Ii. System. Data. SqlClient namespace
The System. Data. SqlClient namespace is the. NET Framework Data provider of SQL Server.
The. NET Framework data provider of SQL Server describes a collection of classes used to access the SQL Server database in a hosted space. You can use SqlDataAdapter to populate the DataSet that reside in the memory. This DataSet can be used to query and update databases.
The main types are as follows:
2.1 System. Data. SqlClient must be referenced before database operations. This class set is used to access the SQL Server database in the hosted space.
2.2 SqlConnection class, indicating an open connection to the SQL Server database.
2.3 SqlCommand class, which indicates a Transact-SQL statement or stored procedure to be executed on the SQL Server database.
2.4 SqlDataAdapter class indicates a set of data commands used to fill the DataSet and update the SQL Server database and connect to a database.
2.5 SqlDataReader class provides a method to read rows from the SQL Server database only into the stream.
Iii. SqlConnection
Before any database operation, you must establish a database connection. SqlConnection indicates an open connection to the SQL Server database.
3.1 connection string: packet size = 4096; data source = data address; persist security info = True; initial catalog = database name; user id = user name; pwd = user password, as shown in
View sourceprint? 1 string sqlcon = "packet size = 4096; data source = 127.0.0.1; persist security info = True; initial catalog = Db_Example; user id = sa ";
3.2 SqlConnection Constructor (String connection String). If a String containing the connection String is specified, a new instance of the SqlConnection class is initialized.
Bytes
View sourceprint? 1 string sqlcon = "packet size = 4096; data source = 127.0.0.1; persist security info = True; initial catalog = Db_Example; user id = sa ";
2 SqlConnection myCon = new SqlConnection (sqlcon );
3.3 SqlConnection. Open Method to Open the database connection.
3.4 SqlConnection. Close method to Close the database connection.
3.5 SqlConnection. Dispose method to release resources.
Iv. SqlCommand
Indicates a Transact-SQL statement or stored procedure to be executed on the SQL Server database. It cannot be inherited.
4.1 SqlCommand Constructor (String query text, SqlConnection a connection to the database instance ).
View sourceprint? 1 SqlCommand sqlcom = new SqlCommand ("insert into tb_user values (Mryoung, 123456)", sqlcon );
4.2 SqlCommand. ExecuteNonQuery method, execute statements such as Transact-SQL INSERT, DELETE, UPDATE, and SET.
4.3 SqlCommand. ExecuteReader method (CommandBehavior), returns a SqlDataReader object. If you create a SqlDataReader and set CommandBehavior to CloseConnection, disabling SqlDataReader will automatically close the connection.
View sourceprint? 1 SqlConnection sqlcon = this. getcon ();
2 SqlCommand sqlcom = new SqlCommand (str_ SQL, sqlcon );
3 sqlcon. Open ();
4 SqlDataReader sqlread = sqlcom. ExecuteReader (CommandBehavior. CloseConnection );
4.4 SqlCommand. Dispose method to release resources.
4.5 SqlCommand. Close method to Close the connection.
V. SqlDataReader
Provides a method to read rows from the SQL Server database only into the stream. This class cannot be inherited. This allows us to get the query results.
5.1 created by the SqlCommand. ExecuteReader method.
5.2 SqlDataReader. Read method: Advances SqlDataReader to the next record, because the default position of SqlDataReader is before the first record. Therefore, you must call Read to access any data. True if multiple rows exist; otherwise, false.
5.3 SqlDataReader. HasRows attribute. Obtain a value that indicates whether SqlDataReader contains one or more rows. True if SqlDataReader contains one or more rows; otherwise, false.
VI. C # general procedure for database operations
VII. Data Query Process
8. login form design
Open VS2008 and create a LoginDemo Project
Add a WINDOWS form named frm_Login and drag the following control:
IX. login process
10. Coding
10.1 Add a database operation class named BaseOperate
10.2 getcon method, returns the database connection object
View sourceprint? 01 /// <summary>
02 // create a database connection
03 // </summary>
04 // <returns> database connection object </returns>
05 public SqlConnection getcon ()
06 {
07 string sqlcon = "packet size = 4096; data source = 127.0.0.1; persist security info = True; initial catalog = Db_Example; user id = sa ";
08 SqlConnection myCon = new SqlConnection (sqlcon );
09 return myCon;
10}
10.3 getcom method: Execute the SQLCOMMAND
View sourceprint? 01 /// <summary>
02 // execute the SQLCOMMAND
03 // </summary>
04 // <param name = "str_ SQL"> SQL statement to be executed </param>
05 public void getcom (string str_ SQL)
06 {
07 SqlConnection sqlcon = this. getcon ();
08 sqlcon. Open ();
09 SqlCommand sqlcom = new SqlCommand (str_ SQL, sqlcon );
10 sqlcom. ExecuteNonQuery ();
11 sqlcom. Dispose ();
12 sqlcon. Close ();
13 & n