sql05 select语句 返回错误列

chengwangtianxia 2015-05-23 10:12:37

public static DataTable ExecuteDataTable(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
{
if (connection == null) throw new ArgumentNullException("connection");

SqlCommand cmd = new SqlCommand();
bool mustCloseConnection = false;
PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection);

using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();

da.Fill(dt);

cmd.Parameters.Clear();

if (mustCloseConnection)
connection.Close();
Cl_Log.LogHelper.DebugDataTable("msdb\r\n"+commandText + "\r\n" + da.SelectCommand.CommandText, dt);
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数据库的代码,现在有个很奇怪的问题:SqlDataAdapter对象填充到DataTable后,我输出日志查看,发现偶尔会出现查询语句和返回DataTable不一致,比如说commandText为"select [a],[b] from [tb_1]",但是返回的DataTable,却是另外一张表的字段和数据
这种情况偶尔会发生,有时候一整天都不会出现,有时候一天内有某几个时间段会频繁发生。
这边的对象,没有采用静态对象,全部是调用时才实例化的。
哪侠大虾能指教下吗?
...全文
184 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
卖水果的net 2015-05-23
  • 打赏
  • 举报
回复
我这个情很奇怪,按理说,错的就一直是错的,对的,就一直是对的。 你的跟踪你的 LogHelper 的输出值了吗?
chengwangtianxia 2015-05-23
  • 打赏
  • 举报
回复
引用 3 楼 ky_min 的回复:
还没看出问题,我觉得可能是弄个地方没处理好,然后并发而引起问题 如果你三个服务器都用这个程序,都有出现问题,那可能性就更大了
现在就是在头疼,连个大概的方向都没有啊,完全不知道该往哪方面去追查。
还在加载中灬 2015-05-23
  • 打赏
  • 举报
回复
还没看出问题,我觉得可能是弄个地方没处理好,然后并发而引起问题

如果你三个服务器都用这个程序,都有出现问题,那可能性就更大了
chengwangtianxia 2015-05-23
  • 打赏
  • 举报
回复
引用 1 楼 ky_min 的回复:
你很明确,语句是这样,而DATATABLE却是另外一张表?
嗯,是的,看下我上传的代码,ExecuteDataTable这个方法,在
da.Fill(dt);
后,我立即输出commandText和dt的列
Cl_Log.LogHelper.DebugDataTable("msdb\r\n"+commandText + "\r\n" + da.SelectCommand.CommandText, dt);
偶尔查询的字段和返回的字段不一样
还在加载中灬 2015-05-23
  • 打赏
  • 举报
回复
你很明确,语句是这样,而DATATABLE却是另外一张表?
chengwangtianxia 2015-05-23
  • 打赏
  • 举报
回复
引用 5 楼 wmxcn2000 的回复:
我这个情很奇怪,按理说,错的就一直是错的,对的,就一直是对的。 你的跟踪你的 LogHelper 的输出值了吗?
输出了跟踪日志,才知道返回的DataTable列和查询语句的列名不同啊。

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧