SqlDataAdapter、DataSet、DataTable使用

Original link: https: //blog.csdn.net/zhang_hui_cs/article/details/7327395

 

using System.Data; 

using System.Data.SqlClient; 

 

// SqlDataAdapter MSDN Web site: http: //msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter.aspx   

// DataTable MSDN Web site: http: //msdn.microsoft.com/en-us/library/system.data.datatable.aspx   

// DataSet MSDN Web site: http://msdn.microsoft.com/en-us/library/system.data.dataset.aspx   

//DataSets, DataTables, and DataViews (ADO.NET): http://msdn.microsoft.com/en-us/library/ss7fbaez.aspx   

namespace Chapter13 

    class FilterSort 

    { 

        static void Main(string[] args) 

        { 

            // connection string   

            string connString = @"   

                                server = .;   

                                integrated security = true;   

                                database = northwind   

                             "; 

 

            // query 1   

            string sql1 = @" select      *    from    customers   ; "; 

 

            // query 2   

            string sql2 = @"     select    *   from   products   where     unitprice < 10    "; 

 

            // combine queries   

            string sql = sql1 + sql2; 

 

            // create connection   

            SqlConnection conn = null; 

 

            try 

            { 

                conn = new SqlConnection(connString); 

                // create data adapter   

                SqlDataAdapter da = new SqlDataAdapter(sql, conn); 

 

                // create and fill data set   

                DataSet ds = new DataSet(); 

 

                /* 

                 * If the sql statement SqlDataAdapter corresponding return a plurality of data sets

                 * The SqlDataAdapter corresponding DataTable name 

                 * Will be named sequentially as "Table", "Table1", "Table2" ...... 

                 * The following call will get the "Table" corresponds to the "Customers",

                 * "Table1" corresponds to the "Products"                  

                 */ 

                da.TableMappings.Add("Table", "Customers"); 

                da.TableMappings.Add("Table1", "Products"); 

 

                da.Fill(ds); 

 

                // get the data tables collection   

                DataTableCollection dtc = ds.Tables

 

                // output DataTableCollection table name in all tables.   

 

                System.Console.WriteLine("DataTable in the DataTableCollection: "); 

                foreach (DataTable dt in dtc

                { 

                    System.Console.WriteLine("\t{0}", dt.ToString()); 

                } 

                // display data from first data table   

                //   

                // display output header   

                System.Console.WriteLine("Results from Customers table:"); 

                System.Console.WriteLine( 

                   "CompanyName".PadRight(20) + 

                   "ContactName".PadLeft(23) + "\n"); 

 

                // set display filter   

                string fl = "country = 'Germany'"; 

 

                // set sort   

                string srt = "companyname asc"; 

                foreach (in the DataRow Row dtc [ "the Customers"]. the Select (FL, srt)// dtc [ "the Customers"] is a DataTable, look for the memory of a database table, ordering inquiry in accordance with the conditions + 

                { 

                    System.Console.WriteLine( 

                       "{0}\t{1}", 

                       row["CompanyName"].ToString().PadRight(25), 

                       row["ContactName"]); 

                } 

 

                // display data from second data table   

                //   

                // display output header   

                System.Console.WriteLine("\n----------------------------"); 

                System.Console.WriteLine("Results from Products table:"); 

                System.Console.WriteLine( 

                   "ProductName".PadRight(20) + 

                   "UnitPrice".PadLeft(21) + "\n"); 

 

                // next sentence dtc [ "Products"] may also be in accordance with the table index value to access, equivalent to dtc [1]   

                foreach (DataRow row in dtc["Products"].Rows) 

                { 

                    System.Console.WriteLine("{0}\t{1}", 

                       row["productname"].ToString().PadRight(25), 

                       row["unitprice"]); 

                } 

            } 

            catch (System.Exception e) 

            { 

                System.Console.WriteLine("Error: " + e); 

            } 

            finally 

            { 

                // close connection   

                conn.Close(); 

            } 

        } 

    } 

}

Guess you like

Origin www.cnblogs.com/wfy680/p/12004524.html