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