首页 技术 正文
技术 2022年11月14日
0 收藏 713 点赞 4,085 浏览 2426 个字
// 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 */

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,565
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,413
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,186
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905