Executing SELECT Statements and TableDirect Commands phần 2

11 311 0
Executing SELECT Statements and TableDirect Commands phần 2

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Listing 8.3: SCHEMAONLYCOMMANDBEHAVIOR.CS /* SchemaOnlyCommandBehavior.cs illustrates how to read a table schema */ using System; using System.Data; using System.Data.SqlClient; class SchemaOnlyCommandBehavior { public static void Main() { SqlConnection mySqlConnection = new SqlConnection( "server=localhost;database=Northwind;uid=sa;pwd=sa" ); SqlCommand mySqlCommand = mySqlConnection.CreateCommand(); mySqlCommand.CommandText = "SELECT ProductID, ProductName, UnitPrice " + "FROM Products " + "WHERE ProductID = 1"; mySqlConnection.Open(); // pass the CommandBehavior.SchemaOnly constant to the // ExecuteReader() method to get the schema SqlDataReader productsSqlDataReader = mySqlCommand.ExecuteReader(CommandBehavior.SchemaOnly); // read the DataTable containing the schema from the DataReader DataTable myDataTable = productsSqlDataReader.GetSchemaTable(); // display the rows and columns in the DataTable foreach (DataRow myDataRow in myDataTable.Rows) { Console.WriteLine("\nNew column details follow:"); foreach (DataColumn myDataColumn in myDataTable.Columns) { Console.WriteLine(myDataColumn + "= " + myDataRow[myDataColumn]); if (myDataColumn.ToString() == "ProviderType") { Console.WriteLine(myDataColumn + "= " + ((System.Data.SqlDbType) myDataRow[myDataColumn])); } } } productsSqlDataReader.Close(); mySqlConnection.Close(); } } You should notice the different details for the ProductID, ProductName, and UnitPrice columns in the output that follows: New column details follow: ColumnName = ProductID ColumnOrdinal = ColumnSize = NumericPrecision = NumericScale = IsUnique = IsKey = BaseCatalogName = BaseColumnName = ProductID BaseSchemaName = BaseTableName = DataType = System.Int32 AllowDBNull = False ProviderType = ProviderType = Int IsAliased = IsExpression = IsIdentity = True IsAutoIncrement = True IsRowVersion = IsHidden = IsLong = False IsReadOnly = True New column details follow: ColumnName = ProductName ColumnOrdinal = ColumnSize = 40 NumericPrecision = NumericScale = IsUnique = IsKey = BaseCatalogName = BaseColumnName = ProductName BaseSchemaName = BaseTableName = DataType = System.String AllowDBNull = False ProviderType = 12 ProviderType = NVarChar IsAliased = IsExpression = IsIdentity = False IsAutoIncrement = False IsRowVersion = IsHidden = IsLong = False IsReadOnly = False New column details follow: ColumnName = UnitPrice ColumnOrdinal = ColumnSize = NumericPrecision = NumericScale = IsUnique = IsKey = BaseCatalogName = BaseColumnName = UnitPrice BaseSchemaName = BaseTableName = DataType = System.Decimal AllowDBNull = True ProviderType = ProviderType = Money IsAliased = IsExpression = IsIdentity = False IsAutoIncrement = False IsRowVersion = IsHidden = IsLong = False IsReadOnly = False Executing a TableDirect Statement Using the ExecuteReader() Method When you set the CommandType property of a Command object to TableDirect, you specify that you want to retrieve all the rows and columns of a particular table You specify the name of the table to retrieve from in the CommandText property Warning SqlCommand objects don't support the CommandType of TableDirect The example in this section will use an OleDbCommand object instead As you know, you can use a SqlConnection object to connect to SQL Server You can also use an OleDbConnection object to connect to SQL Server You simply set the provider to SQLOLEDB in the connection string passed to the OleDbConnection constructor For example: OleDbConnection myOleDbConnection = new OleDbConnection( "Provider=SQLOLEDB;server=localhost;database=Northwind;" + "uid=sa;pwd=sa" ); Next, you create an OleDbConnection object: OleDbCommand myOleDbCommand = myOleDbConnection.CreateCommand(); You then set the CommandType of myOleDbConnection to CommandType.TableDirect: myOleDbCommand.CommandType = CommandType.TableDirect; Next, you specify the name of the table to retrieve from using the CommandText property The following example sets the CommandText property of myOleDbCommand to Products: myOleDbCommand.CommandText = "Products"; You next open the database connection: myOleDbConnection.Open(); Finally, you execute myOleDbCommand using the ExecuteReader() method: OleDbDataReader myOleDbDataReader = myOleDbCommand.ExecuteReader(); The SQL statement actually executed is SELECT * FROM Products, which retrieves all the rows and columns from the Products table Listing 8.4 illustrates the code shown in this section Listing 8.4: EXECUTETABLEDIRECT.CS /* ExecuteTableDirect.cs illustrates how to execute a TableDirect command */ using System; using System.Data; using System.Data.OleDb; class ExecuteTableDirect { public static void Main() { OleDbConnection myOleDbConnection = new OleDbConnection( "Provider=SQLOLEDB;server=localhost;database=Northwind;" + "uid=sa;pwd=sa" ); OleDbCommand myOleDbCommand = myOleDbConnection.CreateCommand(); // set the CommandType property of the OleDbCommand object to // TableDirect myOleDbCommand.CommandType = CommandType.TableDirect; // set the CommandText property of the OleDbCommand object to // the name of the table to retrieve from myOleDbCommand.CommandText = "Products"; myOleDbConnection.Open(); OleDbDataReader myOleDbDataReader = myOleDbCommand.ExecuteReader(); // only read the first rows from the OleDbDataReader object for (int count = 1; count

Ngày đăng: 07/11/2013, 10:15

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan