ADO.NET: SCHEMA information from Data table from DataReader

xiaoxiao2021-03-06  72

Using DataReader's getSchematable () method can get the structure information contained in the data table. This method returns a DataTable, and the COLUMNNAME attribute value of each row in the DataTable is a field name for the data table. SqlConnection Coredb = New SqlConnection (); Coredb.connectionstring = "Workstation ID = /" GQA-Eric-LV / "; packet size = 4096; Integrated Security = SSPI;" "Data Source = /" GQA-ERIC-LV / "; Persist security info = false; initial catalog = coredb";

string mySelectQuery = "SELECT ID, Title FROM myBBS order by id asc"; SqlCommand myCommand = new SqlCommand (mySelectQuery, coreDB); coreDB.Open (); SqlDataReader myReader = myCommand.ExecuteReader (); // define the database information stored In Table Schematable: DataTable Schematable = MyReader.getsChematable (); // Table SCHEMATABLE's information about a field of database table: Response.write ("

"); Foreach DataRow Myrow In schement.rows) {response.write (" "); response.write ("
"); response.write (Myrow ["ColumnName"]); Response.write ("");} response.write "); while (MyReader.read ()) {response.write ("
" MyReader [" ID "]. Tostring () " < / TD> "); response.write (" " myReader [" title "]. Tostring () " < / a> "); response.write (" ");} response.write (" "); myreader.close (); Coredb.close (); display result As shown in the figure: The use of SqlConnection, SqlCommand, DataReader is also exemplified. The table mybbs in the database COREDB are defined as follows: if exists (Select * from dbo.sysObjects where id = Object_id (n '[dbo]. [Mybbs]') And ObjectProperty (ID, n'uSERTABLE ') = 1) Drop Table [ DBO]. [mybbs] Go

转载请注明原文地址:https://www.9cbs.com/read-91680.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.042, SQL: 9