zoukankan      html  css  js  c++  java
  • DataTable && SqlDataReader帮助理解小程序

    // 2015/07/08 
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Data;
    using System.Data.SqlClient;
    
    namespace DataTapleSample
    {
        class Program
        {
            static void Main(string[] args)
            {
                // 通用的专门用来保存数据库中数据的类型
                DataTable table = new DataTable();
    
                // 在DataTable 中保存数据之前,必须先定义结构
                DataColumn stuid =
                    //new DataColumn("StuId",System.Type.GetType("System.Int32"));
                    new DataColumn("StuId",typeof(int));//简写方式和前者等价
                DataColumn stuname = new DataColumn("stuName",typeof(string));
                DataColumn stuaddress = new DataColumn("stuaddress",typeof(string));
    
                // 创建表的结构
                table.Columns.Add(stuid);
                table.Columns.Add(stuname);
                table.Columns.Add(stuaddress);
    
                // 表的约束
                // 主键约束
                table.PrimaryKey = new DataColumn[]{stuid};
                
                // 非空约束
                // stuaddress.AllowDBNull = false;
                // 唯一约束:stuname.Unique;
    
                // 如何在 DataTable 中保存数据
                // DataRow 表示保存在 DataTable 中的一行数据
                DataRow row = table.NewRow();
    
                // 使用 NewRow 方法创建的行,结构与表是相同的(如下三种方法,建议第一种)
                // row[stuid] = 1;
                // row[1] = "XXXX";
                // row[stuaddress] = "XXXX";
                row[stuid] = 7;
                row[stuname] = "XX";
                row[stuaddress] = "XXX";
    
                // 现在加入到 DataTable 中
                table.Rows.Add(row);
    
                // 访问保存在 DataTable 中的数据
                foreach (DataRow r in table.Rows)
                {
                    Console.WriteLine("stuid:{0},stuName:{1},stuaddress:{2}",r[0],r[1],r[2]);
                }
                Console.ReadKey();
            }
        }
    }
    //////////////////////////////////////////////////////////////
    // next
    
    // 2015/07/08
    // DataTable && SqlDataReader
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Data;
    using System.Data.SqlClient;
    
    namespace StuDataTable
    {
        class Program
        {
            static void Main(string[] args)
            {
                // 保存在 DataTable 中
                DataTable table = new DataTable();
    
                string connectionString = "server=.;database=BookSample;uid=sa;pwd=123456";
                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    string sql = "select ID,StuName,Phone from students";
                    SqlCommand cmd = new SqlCommand(sql,connection);
    
                    connection.Open();
    
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    { 
                        // 根据查询结果的结构来创建对应的 DataTable
                        int columnCount = reader.FieldCount; // 查询结果的列数
    
                         // 逐列创建
                        for (int i = 0; i < columnCount; i++)
                        {
                                DataColumn column = new DataColumn(
                                reader.GetName(i),
                                reader.GetFieldType(i)
                                );
                            table.Columns.Add(column);
                        }
    
                        // 逐行从数据库中读取数据
                        while (reader.Read())
                        {
                            DataRow row = table.NewRow();
    
                            for (int i = 0; i < columnCount; i++)
                            { 
                                row[i] = reader[i];
                            }
                            table.Rows.Add(row);
                         }
                    }
    
                  }
                
                // 现在数据库中的数据已经保存到内存中特殊的集合中
                foreach (DataRow row in table.Rows)
                {
                    // 将 ID 读取出来
                    Console.WriteLine(row["ID"]);
                }
                Console.ReadKey();
            }
        }
    }
    
    /*
     相关阅读:
      https://msdn.microsoft.com/zh-cn/library/system.data.datatable.aspx
      https://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqldatareader.aspx
     
     */
    

      

  • 相关阅读:
    互联网时代,80后新时代的管理者
    使用YUI Compressor压缩CSS/JS
    使用最优方法来加速运行你的网站
    随笔 微笑
    首款LGA775平台离子迷你主板登场
    CSS调试技巧五则,兼谈“提问的艺术”
    SharpDevelop 编辑器
    穿越防火墙 让远程桌面自由连接
    开源的搜索引擎工具包和Web搜索引擎系统
    Javascript工具 使用JS Minifier过滤JS文件
  • 原文地址:https://www.cnblogs.com/IamJiangXiaoKun/p/4631316.html
Copyright © 2011-2022 走看看