![Phân tích tư tưởng của nhân dân qua đoạn thơ: Những người vợ nhớ chồng… Những cuộc đời đã hóa sông núi ta trong Đất nước của Nguyễn Khoa Điềm](https://thuvienso.net/upload/document/136415/phan-tich-tu-tuong-cua-nhan-dan-qua-doan-tho-039-039-nhung-nguoi-vo-nho-chong-nhung-cuoc-doi-da-hoa-song-nui-ta-039-039-trong-dat-nuoc-cua-nguyen-khoa-136415.jpg)
Converting a DataReader to a DataSet
Thông tin tài liệu:
Nội dung trích xuất từ tài liệu:
Converting a DataReader to a DataSet[ Team LiB ]Recipe 5.3 Converting a DataReader to a DataSetProblemYou need to transfer data from a DataReader to a DataSet.SolutionCreate a DataTable schema in the destination DataSet using the schema informationreturned by the GetSchemaTable( ) method of the DataReader. Then, use the GetData( )method of the DataReader to load each row of data into an array of objects, and add it tothe DataTable using the Add( ) method of the contained DataRowCollection.The C# code is shown in Example 5-3.Example 5-3. File: ConvertDataReaderToDataSetForm.cs// Namespaces, variables, and constantsusing System;using System.Configuration;using System.Collections;using System.Data;using System.Data.SqlClient;// Table name constantsprivate const String ORDERS_TABLE = Orders;private const String ORDERDETAILS_TABLE = OrderDetails;// Relation name constantsprivate const String ORDERS_ORDERDETAILS_RELATION = Orders_OrderDetails_Relation;// Field name constantsprivate const String ORDERID_FIELD = OrderID;// . . .DataSet ds = new DataSet( );// SQL for batch queryString sqlText = SELECT * FROM Orders; + SELECT * FROM [Order Details];;// Create connection and command.SqlConnection conn = new SqlConnection( ConfigurationSettings.AppSettings[Sql_ConnectString]);SqlCommand cmd = new SqlCommand(sqlText, conn);// Open DataReader with KeyInfo.conn.Open( );SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.KeyInfo);// Loop over the result sets of the DataReader.do{ DataTable schemaTable = dr.GetSchemaTable( ); if (schemaTable != null) { ArrayList pkCols = new ArrayList( ); DataTable dataTable = new DataTable( ); foreach(DataRow schemaRow in schemaTable.Rows) { DataColumn col = new DataColumn( ); col.ColumnName = schemaRow[ColumnName].ToString( ); col.DataType = (Type)schemaRow[DataType]; // set the length of the field for string types only if (schemaRow[DataType].ToString( ) == System.String) col.MaxLength = (Int32)schemaRow[ColumnSize]; col.Unique = (bool)schemaRow[IsUnique]; col.AllowDBNull = (bool)schemaRow[AllowDBNull]; col.AutoIncrement = (bool)schemaRow[IsAutoIncrement]; // If part of the key, add the column name to the // array of columns comprising the primary key. if ((bool)schemaRow[IsKey]) pkCols.Add(col); dataTable.Columns.Add(col); } // Add the primary key to the table. dataTable.PrimaryKey = (DataColumn[])pkCols.ToArray(typeof(DataColumn)); // Add the table to the DataSet. ds.Tables.Add(dataTable); object[] aData = new object[dataTable.Columns.Count]; // Read all rows from the DataReader. while(dr.Read( )) { // Read the row from the DataReader into an array. dr.GetValues(aData); // Add the row from the array to the DataTable. dataTable.Rows.Add(aData); } }}while (dr.NextResult( ));conn.Close( );// Name the tables added to the DataSet.ds.Tables[0].TableName = ORDERS_TABLE;ds.Tables[1].TableName = ORDERDETAILS_TABLE;// Manually add a relation between the tables.ds.Relations.Add(ORDERS_ORDERDETAILS_RELATION, ds.Tables[ORDERS_TABLE].Columns[ORDERID_FIELD], ds.Tables[ORDERDETAILS_TABLE].Columns[ORDERID_FIELD], true);// Bind the Order table default view to the grid.dataGrid.DataSource = ds.Tables[ORDERS_TABLE].DefaultView;DiscussionWhile the DbDataAdapter class—from which DataAdapter classes in .NET providers forrelational databases inherit—defines an overload of the Fill( ) method that converts aDataReader to a DataSet, this method is declared protected and cannot be accessed(unless you write a custom DataAdapter). There is no built-in mechanism for convertingthe connected DataReader object to a disconnected DataTable. Internally, ADO.NETuses the DataReader through the DataAdapter to populate the DataTable object. Thissample demonstrates how this can be done programmatically.Both the SQL Server and OLE DB DataReader expose a GetSchemaTable( ) method thatreturns a table containing the column metadata of the DataReader. You can use this tablein turn to define the DataTable object, into which the DataReader data will be copied.The GetSchemaTable( ) method returns the metadata described in Table 5-1 for eachcolumn. Table 5-1. DataReader GetSchemaTable( ) metadata Column name DescriptionColumnName The name of the column.ColumnOrdinal The zero-based ordinal of the column. The maximum length of a column value. This is the data size ofColumnSize fixed-length data types. The maximum precision of numeric data type columns. Null forNumericPrecision non-numeric data type columns. The number of digits to the right of the decimal forNumericScale DBTYPE_DECIMAL or DBTYPE_NUMERIC data type columns. Otherwise, null. Indicates whether the value in the column must be unique within allIsUnique records. Indicates whether the column is part of the primary key or a uniqueIsKey key uniquely identifying the record.BaseServerName The instance name of the data server used by the DataReader.BaseCatalogName The name of the catalog in the data store.BaseColumnName The name of the column in the data store.BaseSchemaName The name of the schema in the data store.BaseTableName The name of the table in the data store.DataType The .NET Framework data type of the column.AllowDBNull Indicates whether null values are allowed in the column.ProviderType The .NET data provider data type of the column.IsAlias ...
Tìm kiếm theo từ khóa liên quan:
công nghệ thông tin kỹ thuật lập trình Oreilly Ado Dot Net Cookbook Ebook-Lib Converting a DataReader to a DataSetTài liệu có liên quan:
-
52 trang 462 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 361 0 0 -
96 trang 329 0 0
-
74 trang 328 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 316 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 311 1 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 303 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 303 0 0 -
Tài liệu hướng dẫn sử dụng thư điện tử tài nguyên và môi trường
72 trang 296 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 290 0 0 -
64 trang 288 0 0
-
Bài giảng An toàn và bảo mật thông tin - Trường đại học Thương Mại
31 trang 270 0 0 -
LUẬN VĂN: TÌM HIỂU PHƯƠNG PHÁP HỌC TÍCH CỰC VÀ ỨNG DỤNG CHO BÀI TOÁN LỌC THƯ RÁC
65 trang 258 0 0 -
47 trang 257 0 0
-
Giáo trình Hệ điều hành: Phần 2
53 trang 252 0 0 -
NGÂN HÀNG CÂU HỎI TRẮC NGHIỆM THIẾT KẾ WEB
8 trang 244 0 0 -
Giáo trình Autocad - Nghề: Quản trị mạng máy tính - Trình độ: Cao đẳng nghề (Phần 2)
52 trang 229 0 0 -
63 trang 227 0 0
-
83 trang 224 0 0
-
Các phương pháp nâng cấp cho Windows Explorer trong Windows
5 trang 224 0 0