zoukankan      html  css  js  c++  java
  • DataView使用

    using System;
    using System.Data;
    using System.Data.SqlClient;
    
    //DataView的MSDN参考网站:http://msdn.microsoft.com/en-us/library/8sd1cd0a.aspx
    namespace Chapter13
    {
        class DataViews
        {
            static void Main(string[] args)
            {
                // connection string
                string connString = @"
                                server = .;
                                integrated security = true;
                                database = northwind
                             ";
    
                // query
                string sql = @"
                        select
                           contactname,
                           country
                        from
                           customers
                     ";
    
                // create connection
                SqlConnection conn = null;
    
                try
                {
                    conn = new SqlConnection(connString);
                    // Create data adapter
                    SqlDataAdapter da = new SqlDataAdapter();
                    da.SelectCommand = new SqlCommand(sql, conn);
    
                    // create and fill dataset
                    DataSet ds = new DataSet();
                    da.Fill(ds, "customers");
    
                    // get data table reference
                    DataTable dt = ds.Tables["customers"];
    
                    // create data view
                    DataView dv = new DataView(
                       dt,
                       "country = 'Germany'",
                       "country",
                       DataViewRowState.CurrentRows
                    );
    
                    // display data from data view
                    foreach (DataRowView drv in dv)
                    {
                        for (int i = 0; i < dv.Table.Columns.Count; i++)
                            Console.Write(drv[i] + "\t");
                        Console.WriteLine();
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error: " + e);
                }
                finally
                {
                    // close connection
                    conn.Close();
                }
            }
        }
    }
    
    
    
    //所有代码来自书籍《Begining C# Databases From Novice to Professional》

  • 相关阅读:
    死锁
    不能复制文件到服务器
    JWT
    身份验证
    依赖注入
    ml.net
    swift 枚举、结构、类
    nginx 负载均衡
    sql 时间函数大全
    更新SVN时提示要清理,但清理失败,乱码得解决方案
  • 原文地址:https://www.cnblogs.com/java20130722/p/3207188.html
Copyright © 2011-2022 走看看