VS. NET (C #) Database Interface: SqlCommand object generation and application case sharing, vs.net
Before creating a Command object, you need to clarify two things:
1. Which data source to operate on?
2. What operations are performed?
1. Specify the connected Data source: create a Connection object.
Before using the SqlCommand object, you must first determine a SqlConnection object for data transmission with the SQL Server database.
Generate the SqlConnection object. The Code is as follows:
// Construct the connection string SqlConnectionStringBuilder connStr = new SqlConnectionStringBuilder (); connStr. dataSource = "192.168.1.20"; connStr. initialCatalog = "rzerp_zjsy"; connStr. userID = "developer"; connStr. password = "developer"; SqlConnection conn = new SqlConnection (); // create a connection object conn. connectionString = connStr. connectionString; // set the connection string
2. execute the operation: Create a SqlCommand object
Create a SqlCommand object using the following methods:
※Use constructors to generate SqlCommand objects
※Call the CreateCommand () method of the SqlConnection object to generate the SqlCommand object.
2.1. Use constructors to generate SqlCommand objects
SqlCommand ()? ? // Simple Constructor (No parameter)
SqlCommand (string commandText)
SqlCommand (string commandText, SqlConnection mySqlConnection)
Note: commandText contains SQL statements, stored procedure calls, or tables to be read. mySqlConnection is the corresponding SqlConnection object.
Code:
SqlCommand cmd = new SqlCommand (); // generate the SqlCommand object cmd. Connection = conn; // set the Connection attribute of the cmd object to conn.
The CommandType attribute determines the command type to be executed. Use the System. Data. CommandType enumerated value to specify the CommandType attribute.
The enumerated value of CommandType is as follows:
Value |
Description |
Text |
Indicates that the command is an SQL statement and the default value is Text. |
StoredProcedure |
Indicates that the command is called during the storage process. |
TableDirect |
The name of the read row and column. Note: SqlCommand objects do not support TableDirect. You must use other Command objects. |
Run the following code to query an SQL statement:
// Execute an SQL query cmd. commandText = "select * from aaa"; cmd. connection = conn; cmd. commandType = CommandType. text; cmd. execute (); // start execution
Run the following code to query a stored procedure:
SqlCommand cmd = new SqlCommand ("GetEmpolyees", conn); cmd. Connection = conn; cmd. CommandType = CommandType. StoredProcedure; cmd. Execute (); // start execution
2.2, CreateCommand () method generates SqlCommand object
// Use the CreateCommand () method to generate the SqlCommand object SqlCommand cmd = conn. CreateCommand ();
2.3. The SqlCommand object has a parameter constructor.
String plain Text = "select * from aaa"; SqlCommand cmd = new SqlCommand (plain Text, conn); cmd. CommandType = CommandType. Text; cmd. Execute (); // start execution