using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
namespace _14__17_SqlDataReader_实例
{
class Program
{
static void Main(string[] args)
{
SqlConnection cnn = new SqlConnection("server=.;uid=sa;pwd=system;database=E_TestData");
try
{
cnn.Open();
SqlCommand cmd = new SqlCommand(@"select * from TDATA WHERE Date_Time like '2018/03/01%'",cnn);
SqlDataReader sdr = cmd.ExecuteReader();
Console.WriteLine("使用SqlDataReader列印输出:");
for(int i=0;i<sdr.FieldCount;i++)
{
Console.Write(sdr.GetName(i)+"\t");
}
Console.WriteLine();
while(sdr.Read())
{
for (int n = 0; n < sdr.FieldCount; n++)
Console.Write(sdr[n].ToString()+"\t");
Console.WriteLine();
}
sdr.Close();
Console.WriteLine("使用SqlDataAdapter和DataSet列印输出:");
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds,"testdata");
foreach(DataTable dt1 in ds.Tables)
{
foreach(DataColumn dc1 in dt1.Columns)
{
Console.Write(dc1.ColumnName+"\t");
}
Console.WriteLine();
foreach(DataRow dr1 in dt1.Rows)
{
foreach(DataColumn dc2 in dt1.Columns)
{
Console.Write(dr1[dc2] + "\t");
}
Console.WriteLine();
}
}
Console.ReadKey();
}
catch(Exception ex)
{
Console.WriteLine(ex.Message.ToString());
}
finally
{
cnn.Close();
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
namespace _14__17_SqlDataReader_实例
{
class Program
{
static void Main(string[] args)
{
SqlConnection cnn = new SqlConnection("server=.;uid=sa;pwd=system;database=E_TestData");
try
{
cnn.Open();
SqlCommand cmd = new SqlCommand(@"select * from TDATA WHERE Date_Time like '2018/03/01%'",cnn);
SqlDataReader sdr = cmd.ExecuteReader();
Console.WriteLine("使用SqlDataReader列印输出:");
for(int i=0;i<sdr.FieldCount;i++)
{
Console.Write(sdr.GetName(i)+"\t");
}
Console.WriteLine();
while(sdr.Read())
{
for (int n = 0; n < sdr.FieldCount; n++)
Console.Write(sdr[n].ToString()+"\t");
Console.WriteLine();
}
sdr.Close();
Console.WriteLine("使用SqlDataAdapter和DataSet列印输出:");
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds,"testdata");
foreach(DataTable dt1 in ds.Tables)
{
foreach(DataColumn dc1 in dt1.Columns)
{
Console.Write(dc1.ColumnName+"\t");
}
Console.WriteLine();
foreach(DataRow dr1 in dt1.Rows)
{
foreach(DataColumn dc2 in dt1.Columns)
{
Console.Write(dr1[dc2] + "\t");
}
Console.WriteLine();
}
}
Console.ReadKey();
}
catch(Exception ex)
{
Console.WriteLine(ex.Message.ToString());
}
finally
{
cnn.Close();
}
}
}
}