DataTable 和 SqlDataReader 的关系


static void Main()
{
    string sql = @"
SELECT FID,FAccount,FCompanyID,FStatus FROM dbo.TAccounts
WHERE FCompanyID=@FCompanyId";

    IList<SqlParameter> paramList = new List<SqlParameter>();
    SqlParameter param = new SqlParameter("@FCompanyId", 36052);
    paramList.Add(param);

    DataTable dt = new DataTable();
    dt.TableName = "TAccounts";

    SqlConnection connection = null;
    SqlCommand command = null;
    SqlDataReader reader = null;
    try
    {
        string connectionString = AdoConfig.connectionString;
        connection = new SqlConnection(connectionString);

        command = new SqlCommand(sql, connection) { CommandType = CommandType.Text };
        for (int i = 0; i < paramList.Count; i++)
        {
            int index = i;
            command.Parameters.Add(paramList[index]);
        }

        connection.Open();

        reader = command.ExecuteReader(CommandBehavior.Default);
        if (reader != null) //if (reader != null && reader.HasRows == true)
        {
            dt.Load(reader, LoadOption.PreserveChanges, null);
        }
        //有数据时
        /*
reader
{System.Data.SqlClient.SqlDataReader}
base: {System.Data.SqlClient.SqlDataReader}
Depth: 0
FieldCount: 4
HasRows: true
IsClosed: false
RecordsAffected: -1
VisibleFieldCount: 4                   
            */
    }
    finally
    {
        if (reader != null) { /* reader.Close(); */ reader.Dispose(); }
        if (command != null)
        {
            command.Parameters.Clear();
            command.Dispose();
        }
        if (connection != null)
        {
            connection.Close();
            connection.Dispose();
        }
    }
    Console.WriteLine(dt == null);

    Console.WriteLine("\n执行完成");
    Console.ReadLine();
}

 

 //dt.Load(reader, LoadOption.PreserveChanges, null);

 

dt.Load(reader, LoadOption.PreserveChanges, null);

 

 

static void Main()
{
    string sql = @"
SELECT FID,FAccount,FCompanyID,FStatus FROM dbo.TAccounts
WHERE FCompanyID=@FCompanyId";

    IList<SqlParameter> paramList = new List<SqlParameter>();
    SqlParameter param = new SqlParameter("@FCompanyId", 360520);
    paramList.Add(param);

    DataTable dt = new DataTable();
    dt.TableName = "TAccounts";

    SqlConnection connection = null;
    SqlCommand command = null;
    SqlDataReader reader = null;
    try
    {
        string connectionString = AdoConfig.connectionString;
        connection = new SqlConnection(connectionString);

        command = new SqlCommand(sql, connection) { CommandType = CommandType.Text };
        for (int i = 0; i < paramList.Count; i++)
        {
            int index = i;
            command.Parameters.Add(paramList[index]);
        }

        connection.Open();

        reader = command.ExecuteReader(CommandBehavior.Default);
        if (reader != null) //if (reader != null && reader.HasRows == true)
        {
            dt.Load(reader, LoadOption.PreserveChanges, null);
        }
        //无数据时
        /*
reader
{System.Data.SqlClient.SqlDataReader}
base: {System.Data.SqlClient.SqlDataReader}
Depth: 0
FieldCount: 4
HasRows: false
IsClosed: false
RecordsAffected: -1
VisibleFieldCount: 4                 
            */
    }
    finally
    {
        if (reader != null) { /* reader.Close(); */ reader.Dispose(); }
        if (command != null)
        {
            command.Parameters.Clear();
            command.Dispose();
        }
        if (connection != null)
        {
            connection.Close();
            connection.Dispose();
        }
    }
    Console.WriteLine(dt == null);

    Console.WriteLine("\n执行完成");
    Console.ReadLine();
}

 

 

 

//无数据时
if (reader != null && reader.HasRows == true)
{
    dt.Load(reader, LoadOption.PreserveChanges, null);
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM