读书人

一个很奇怪的有关问题大家来帮帮忙。

发布时间: 2012-10-10 13:58:11 作者: rapoo

一个很奇怪的问题,大家来帮帮忙。。。
一个存储过程,我用程序执行存储过程查询出一个dataTable里面是71条数据,但是我直接在数据库中执行的话,返回的数据条数是79条,这个问题好奇怪啊。。。上代码:

C# code
 public static DataTable WhiteCardReplacementData(string beginDate, string enddate,string dateType)        {            DataTable dt = new DataTable();            SqlParameter[] para = new SqlParameter[] {                 new SqlParameter("@BeginDate",beginDate),                new SqlParameter("@EndDate",enddate),                new SqlParameter("@DateType",dateType)            };            try            {                dt = SqlHelper.ExecuteDataTable(SqlHelper.ConnectionString, CommandType.StoredProcedure, "usp_Report_WhiteCardData", para);                return dt;            }            catch (Exception e)            {                                throw;            }        }//Sqlhelper中的代码: public static DataTable ExecuteDataTable(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)        {            if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");            // Create & open a SqlConnection, and dispose of it after we are done            using (SqlConnection connection = new SqlConnection(connectionString))            {                connection.Open();                // Call the overload that takes a connection in place of the connection string                return ExecuteDataTable(connection, commandType, commandText, commandParameters);            }        }public static DataTable ExecuteDataTable(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)        {            if (connection == null) throw new ArgumentNullException("connection");            // Create a command and prepare it for execution            SqlCommand cmd = new SqlCommand();            bool mustCloseConnection = false;            PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection);            // Create the DataAdapter & DataSet            using (SqlDataAdapter da = new SqlDataAdapter(cmd))            {                DataTable dt = new DataTable();                // Fill the DataSet using default values for DataTable names, etc                da.Fill(dt);                // Detach the SqlParameters from the command object, so they can be used again                cmd.Parameters.Clear();                if (mustCloseConnection)                    connection.Close();                // Return the datatable                return dt;            }        }private static void PrepareCommand(SqlCommand command, SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, SqlParameter[] commandParameters, out bool mustCloseConnection)        {            if (command == null) throw new ArgumentNullException("command");            if (commandText == null || commandText.Length == 0) throw new ArgumentNullException("commandText");            // If the provided connection is not open, we will open it            if (connection.State != ConnectionState.Open)            {                mustCloseConnection = true;                connection.Open();            }            else            {                mustCloseConnection = false;            }            // Associate the connection with the command            command.Connection = connection;            // Set the command text (stored procedure name or SQL statement)            command.CommandText = commandText;            // If we were provided a transaction, assign it            if (transaction != null)            {                if (transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");                command.Transaction = transaction;            }            // Set the command type            command.CommandType = commandType;            // Attach the command parameters if they are provided            if (commandParameters != null)            {                AttachParameters(command, commandParameters);            }            return;        } 


附上SQL语句:
SQL code
select '邮编:'+ZipCode as ZipCode,Address,Customer         from dbo.WhiteCardReplacementData        where convert(varchar(10),UpdateDate,120) between @BeginDate and @EndDate        and Customer!=''

谢谢啦

[解决办法]
确定操作的是同一个数据库?
[解决办法]
UpdateDate用时间做条件,不要转换为字符
[解决办法]
debug,看看你传的参数对不对
[解决办法]
那条数据是正确的 呢?
[解决办法]
查询条件 出错了吧!

[解决办法]
看下传入的参数
[解决办法]
看查询条件,
把你程序里查询亮语句输出,
然后到 sql 里去执行

读书人网 >C#

热点推荐