Creating a SqlCommand Object
Số trang: 3
Loại file: pdf
Dung lượng: 24.33 KB
Lượt xem: 1
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Use one of the SqlCommand constructors. Call the CreateCommand() method of a SqlConnection object
Nội dung trích xuất từ tài liệu:
Creating a SqlCommand ObjectCreating a SqlCommand ObjectThere are two ways you can create a SqlCommand object: • Use one of the SqlCommand constructors. • Call the CreateCommand() method of a SqlConnection object.Youll see how to use both these ways to create SqlCommand objects next.Note You can use the same ways shown in the following sections to create an OleDbCommand or OdbcCommand object.Creating a SqlCommand Object Using a ConstructorThe SqlCommand constructors are as follows:SqlCommand()SqlCommand(string commandText)SqlCommand(string commandText, SqlConnection mySqlConnection)SqlCommand(string commandText, SqlConnection mySqlConnection, SqlTransactionmySqlTransaction)wherecommandText contains your SQL statement, stored procedure call, or table to retrievefrom.mySqlConnection is your SqlConnection object.mySqlTransaction is your SqlTransaction object.Before you use a SqlCommand object you first need a SqlConnection object, which isused to communicate with a SQL Server database:mySqlConnection.ConnectionString = server=localhost;database=Northwind;uid=sa;pwd=sa;Next, you can create a new SqlCommand object using the following statement:SqlCommand mySqlCommand = new SqlCommand();You then set the Connection property of mySqlCommand to mySqlConnection:mySqlCommand.Connection = mySqlConnection;The mySqlCommand object will then use mySqlConnection to communicate with thedatabase.Now, the CommandType property of a Connection object determines the type ofcommand to be executed. You can use any of the values in theSystem.Data.CommandType enumeration to specify the CommandType property. Table8.3 shows the CommandType enumeration values. Table 8.3: CommandType ENUMERATION VALUESVALUE DESCRIPTIONText Indicates the command is a SQL statement. Text is the default.StoredProcedure Indicates the command is a stored procedure call.TableDirect Indicates the name of a table, for which all rows and columns are to be retrieved. Note: SqlCommand objects dont support TableDirect. You have to use an object of one of the other Command classes instead.Youll see how to use all three of these command types in this chapter. For now, Ill focuson the default Text command type, which indicates the command is a SQL statement.You set the command to be executed using the CommandText property of yourCommand object. The following example sets the CommandText property ofmySqlCommand to a SELECT statement:mySqlCommand.CommandText = SELECT TOP 10 CustomerID, CompanyName, ContactName, Address + FROM Customers + ORDER BY CustomerID;You can also pass the command and the Connection object to the constructor in one stepwhen creating a Command object. For example:SqlCommand mySqlCommand = new SqlCommand( SELECT TOP 5 CustomerID, CompanyName, ContactName, Address + FROM Customers + ORDER BY CustomerID, mySqlConnection);In the next section, youll learn how to create a SqlCommand object using theCreateCommand() method of a SqlConnection object.Creating a SqlCommand Object Using the CreateCommand() MethodRather than creating a SqlCommand object using the constructors, you can use theCreateCommand() method of a SqlConnection object. The CreateCommand() methodreturns a new SqlCommand object. For example:SqlCommand mySqlCommand = mySqlConnection.CreateCommand();The mySqlCommand object will use mySqlConnection to communicate with thedatabase.
Nội dung trích xuất từ tài liệu:
Creating a SqlCommand ObjectCreating a SqlCommand ObjectThere are two ways you can create a SqlCommand object: • Use one of the SqlCommand constructors. • Call the CreateCommand() method of a SqlConnection object.Youll see how to use both these ways to create SqlCommand objects next.Note You can use the same ways shown in the following sections to create an OleDbCommand or OdbcCommand object.Creating a SqlCommand Object Using a ConstructorThe SqlCommand constructors are as follows:SqlCommand()SqlCommand(string commandText)SqlCommand(string commandText, SqlConnection mySqlConnection)SqlCommand(string commandText, SqlConnection mySqlConnection, SqlTransactionmySqlTransaction)wherecommandText contains your SQL statement, stored procedure call, or table to retrievefrom.mySqlConnection is your SqlConnection object.mySqlTransaction is your SqlTransaction object.Before you use a SqlCommand object you first need a SqlConnection object, which isused to communicate with a SQL Server database:mySqlConnection.ConnectionString = server=localhost;database=Northwind;uid=sa;pwd=sa;Next, you can create a new SqlCommand object using the following statement:SqlCommand mySqlCommand = new SqlCommand();You then set the Connection property of mySqlCommand to mySqlConnection:mySqlCommand.Connection = mySqlConnection;The mySqlCommand object will then use mySqlConnection to communicate with thedatabase.Now, the CommandType property of a Connection object determines the type ofcommand to be executed. You can use any of the values in theSystem.Data.CommandType enumeration to specify the CommandType property. Table8.3 shows the CommandType enumeration values. Table 8.3: CommandType ENUMERATION VALUESVALUE DESCRIPTIONText Indicates the command is a SQL statement. Text is the default.StoredProcedure Indicates the command is a stored procedure call.TableDirect Indicates the name of a table, for which all rows and columns are to be retrieved. Note: SqlCommand objects dont support TableDirect. You have to use an object of one of the other Command classes instead.Youll see how to use all three of these command types in this chapter. For now, Ill focuson the default Text command type, which indicates the command is a SQL statement.You set the command to be executed using the CommandText property of yourCommand object. The following example sets the CommandText property ofmySqlCommand to a SELECT statement:mySqlCommand.CommandText = SELECT TOP 10 CustomerID, CompanyName, ContactName, Address + FROM Customers + ORDER BY CustomerID;You can also pass the command and the Connection object to the constructor in one stepwhen creating a Command object. For example:SqlCommand mySqlCommand = new SqlCommand( SELECT TOP 5 CustomerID, CompanyName, ContactName, Address + FROM Customers + ORDER BY CustomerID, mySqlConnection);In the next section, youll learn how to create a SqlCommand object using theCreateCommand() method of a SqlConnection object.Creating a SqlCommand Object Using the CreateCommand() MethodRather than creating a SqlCommand object using the constructors, you can use theCreateCommand() method of a SqlConnection object. The CreateCommand() methodreturns a new SqlCommand object. For example:SqlCommand mySqlCommand = mySqlConnection.CreateCommand();The mySqlCommand object will use mySqlConnection to communicate with thedatabase.
Tìm kiếm theo từ khóa liên quan:
kĩ thuật lập trình công nghệ thông tin lập trình ngôn ngữ lập trình C Shark C# sybex - c.sharp database programming Creating a SqlCommand ObjectGợi ý tài liệu liên quan:
-
52 trang 429 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 310 0 0 -
74 trang 294 0 0
-
96 trang 289 0 0
-
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 288 0 0 -
Đồ án tốt nghiệp: Xây dựng ứng dụng di động android quản lý khách hàng cắt tóc
81 trang 277 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 272 0 0 -
Giáo trình Lập trình hướng đối tượng: Phần 2
154 trang 271 0 0 -
Tài liệu dạy học môn Tin học trong chương trình đào tạo trình độ cao đẳng
348 trang 269 1 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 261 0 0