14-11读取数据库-DataReader(1)
1、HasRows
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;//引用命名空间
namespace _14_11读取数据库_DataReader_1_
{
class Program
{
static void Main(string[] args)
{
string constr = "Server=.;user=sa;pwd=sa;database=csharpzxw";
SqlConnection mycon = new SqlConnection(constr);
try
{
mycon.Open();
string sql = "select * from mytable001";
SqlCommand mycom = new SqlCommand(sql, mycon);
//声明datareader
SqlDataReader mydr;
mydr = mycom.ExecuteReader();
if (mydr.HasRows)
{
Console.WriteLine("mytable001中存在数据");
Console.ReadKey();//延长黑屏显示时间
}
else
{
Console.WriteLine("mytable001不存在任何数据");
Console.ReadKey();//延长黑屏显示时间
}
mydr.Close();
}
catch(Exception ex)
{
Console.WriteLine(ex.Message.ToString());
}
finally
{
mycon.Close();
}
}
}
}
2、FieldCount
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;//引用命名空间
namespace _14_11读取数据库_DataReader_1_
{
class Program
{
static void Main(string[] args)
{
string constr = "Server=.;user=sa;pwd=sa;database=csharpzxw";
SqlConnection mycon = new SqlConnection(constr);
try
{
mycon.Open();
string sql = "select * from mytable001";
SqlCommand mycom = new SqlCommand(sql, mycon);
//声明datareader
SqlDataReader mydr;
mydr = mycom.ExecuteReader();
if (mydr.HasRows)
{
Console.WriteLine("mytable001中存在数据");
}
else
{
Console.WriteLine("mytable001不存在任何数据");
}
Console.WriteLine(mydr.FieldCount);
mydr.Close();
Console.ReadKey();//延长黑屏显示时间
}
catch(Exception ex)
{
Console.WriteLine(ex.Message.ToString());
}
finally
{
mycon.Close();
}
}
}
}
3、IsClosed
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;//引用命名空间
namespace _14_11读取数据库_DataReader_1_
{
class Program
{
static void Main(string[] args)
{
string constr = "Server=.;user=sa;pwd=sa;database=csharpzxw";
SqlConnection mycon = new SqlConnection(constr);
try
{
mycon.Open();
string sql = "select * from mytable001";
SqlCommand mycom = new SqlCommand(sql, mycon);
//声明datareader
SqlDataReader mydr;
mydr = mycom.ExecuteReader();
if (mydr.HasRows)
{
Console.WriteLine("mytable001中存在数据");
}
else
{
Console.WriteLine("mytable001不存在任何数据");
}
Console.WriteLine(mydr.FieldCount);
Console.WriteLine(mydr.IsClosed);
mydr.Close();
Console.WriteLine(mydr.IsClosed);
Console.ReadKey();//延长黑屏显示时间
//Console.WriteLine(mydr.IsClosed);//放在上一条语句Console.ReadKey();后面,显示不出来条语句是否执行
}
catch(Exception ex)
{
Console.WriteLine(ex.Message.ToString());
}
finally
{
mycon.Close();
}
}
}
}
4、GetDataTypeName
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;//引用命名空间
namespace _14_11读取数据库_DataReader_1_
{
class Program
{
static void Main(string[] args)
{
string constr = "Server=.;user=sa;pwd=sa;database=csharpzxw";
SqlConnection mycon = new SqlConnection(constr);
try
{
mycon.Open();
string sql = "select * from mytable001";
SqlCommand mycom = new SqlCommand(sql, mycon);
//声明datareader
SqlDataReader mydr;
mydr = mycom.ExecuteReader();
if (mydr.HasRows)
{
Console.WriteLine("mytable001中存在数据");
}
else
{
Console.WriteLine("mytable001不存在任何数据");
}
//Console.WriteLine(mydr.FieldCount);
//Console.WriteLine(mydr.IsClosed);
Console.WriteLine( mydr.GetDataTypeName(1));
mydr.Close();
Console.WriteLine(mydr.IsClosed);
Console.ReadKey();//延长黑屏显示时间
//Console.WriteLine(mydr.IsClosed);//放在上一条语句Console.ReadKey();后面,显示不出来条语句是否执行
}
catch(Exception ex)
{
Console.WriteLine(ex.Message.ToString());
}
finally
{
mycon.Close();
}
}
}
}
5、GetName
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;//引用命名空间
namespace _14_11读取数据库_DataReader_1_
{
class Program
{
static void Main(string[] args)
{
string constr = "Server=.;user=sa;pwd=sa;database=csharpzxw";
SqlConnection mycon = new SqlConnection(constr);
try
{
mycon.Open();
string sql = "select * from mytable001";
SqlCommand mycom = new SqlCommand(sql, mycon);
//声明datareader
SqlDataReader mydr;
mydr = mycom.ExecuteReader();
if (mydr.HasRows)
{
Console.WriteLine("mytable001中存在数据");
}
else
{
Console.WriteLine("mytable001不存在任何数据");
}
//Console.WriteLine(mydr.FieldCount);
//Console.WriteLine(mydr.IsClosed);
//Console.WriteLine( mydr.GetDataTypeName(1));
Console.WriteLine(mydr.GetName(2));
mydr.Close();
Console.WriteLine(mydr.IsClosed);
Console.ReadKey();//延长黑屏显示时间
//Console.WriteLine(mydr.IsClosed);//放在上一条语句Console.ReadKey();后面,显示不出来条语句是否执行
}
catch(Exception ex)
{
Console.WriteLine(ex.Message.ToString());
}
finally
{
mycon.Close();
}
}
}
}