ADO.NET中的一点小疑惑

来源:百度知道 编辑:UC知道 时间:2024/09/21 17:52:19
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;//加载空间

namespace Lesson1
{
class Program
{
static void Main(string[] args)
{
//创建连接
SqlConnection cnn = new SqlConnection();
// 设置字符串连接
cnn.ConnectionString = "Data Source=LENOVO-4B9BD0F2;Initial Catalog=Student;Integrated Security=True";
//打开连接
cnn.Open();
//创建Commend连接
SqlCommand cmd = new SqlCommand();
cmd.Connection = cnn;
cmd.CommandText = "select * from StuInfo";
// 获取读写器
SqlDataReader dr = cmd.ExecuteReader();
// 循环读取数据
while(dr.Read())
{
Console.WriteLine("{0,-6},{1,-10}",dr[0],dr[1]);
}

先看这个:
Console.WriteLine("{0},{1}",dr[0],dr[1]);
表示以 "第1个变量,第2个变量" 的格式输出.dr[0],dr[1]就是表示你 重数据库读到的纪录.
{0,-6}{1,-10},好像是表示你的占位吧 这个不能确定:表示第1个占6字节,第2变量占10字节.
while(dr.Read())
这个语句会一直读到返回纪录为空为止,当然就是遍历你表中的所有数据.