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
 
 */

  

原文地址:https://www.cnblogs.com/IamJiangXiaoKun/p/4631316.html