VS. NET (C #) Database Interface: SqlCommand object generation and application case sharing, vs.net

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.