Use c# to link the database

1. Create a connection string
string a = "Data Source=.;Initial Catalog=QQ;Integrated Security=True";
1
2. Create a connection object
SqlConnection connection = new SqlConnection(a);
1
Execute a SQL statement
1. Will think The SQL statement to be executed is placed in a variable
2. The execution of the SQL statement requires an executor. Create an enforcer (instantiate the Command object)
SqlCommand cmd = new SqlCommand (install the SQL statement variable, connection object);
1
Note: in The Connection must be opened before execution. The
connection object. Open(); Open

Execute the SQL statement
ExecuteNonQuery() of the command object ;
returns an int type variable. If the SQL statement is to operate on database records (such as adding, deleting and updating records), then the method will return the number of records affected by the operation.

If the SQL statement is an operation on the records of the database (such as adding, deleting and updating records), then the method will return the number of records affected by the operation.
ExecuteScalar();
executes the SQL statement of the command object. If the SQL statement is a SELECT query, only the first row and the first column in the query result set are returned, and other rows and columns are ignored. The result returned by this method is of object type, which must be cast to the required type before being used. If the SQL statement is not a SELECT query, the returned result has no effect.

If the SQL statement is a SELECT query, only the first row and the first column
ExecuteReader() in the query result set will be returned ;
the SQL statement that executes the command object, in ADO.NET, is the ExecuteReader() method of the DataReader object to perform the data column Out, and we use the ExecuteReader() method to display data is the fastest way, because when we use the DataReader object in the ExecuteReader() method to display the data in website construction, he can only display one by one Read forward and cannot return, that is, like the Movenext of the Recordset object in the ADO method in ASP, it does not have a return method such as move -1.

Guess you like

Origin blog.csdn.net/qq_50722361/article/details/109791475