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();
            }
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值