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