OdbcDataAdapter填充DataTable时出现“尝试读取或写入受保护的内存”

sliang28 2016-02-03 11:03:57
下面函数可以正常调用2次,第三次调用时出现标题异常。
调用时只是sql字符串中变量发生改变而已。
在自己电脑中怎么运行都没有问题,但是Debug放在其它电脑上运行就异常,奇怪的是可以正常调用2次,第三次不行了。
请各位老师给分析下什么问题导致。
函数如下:
[code=csharp][/public DataTable ExecuteDataTable(string commandText, CommandType type)
{
OdbcCommand cmd = new OdbcCommand();
cmd.CommandText = commandText;
cmd.CommandType = type;
cmd.Connection = Conn;
OdbcDataAdapter dr = new OdbcDataAdapter(cmd);
DataTable dt = new DataTable();
dr.Fill(dt);
cmd.Connection.Close();
return dt;
}code]
异常详细信息如下:
[code=csharp][/未处理System.AccessViolationException
Message=尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
Source=System.Data
StackTrace:
在 System.Data.Common.UnsafeNativeMethods.SQLColAttributeW(OdbcStatementHandle StatementHandle, Int16 ColumnNumber, Int16 FieldIdentifier, CNativeBuffer CharacterAttribute, Int16 BufferLength, Int16& StringLength, IntPtr& NumericAttribute)
在 System.Data.Odbc.OdbcStatementHandle.ColumnAttribute(Int32 columnNumber, Int16 fieldIdentifier, CNativeBuffer characterAttribute, Int16& stringLength, SQLLEN& numericAttribute)
在 System.Data.Odbc.OdbcDataReader.GetColAttributeStr(Int32 i, SQL_DESC v3FieldId, SQL_COLUMN v2FieldId, HANDLER handler)
在 System.Data.Odbc.OdbcDataReader.GetName(Int32 i)
在 System.Data.ProviderBase.SchemaMapping.GenerateFieldNames(DataReaderContainer dataReader)
在 System.Data.ProviderBase.SchemaMapping..ctor(DataAdapter adapter, DataSet dataset, DataTable datatable, DataReaderContainer dataReader, Boolean keyInfo, SchemaType schemaType, String sourceTableName, Boolean gettingData, DataColumn parentChapterColumn, Object parentChapterValue)
在 System.Data.Common.DataAdapter.FillMappingInternal(DataSet dataset, DataTable datatable, String srcTable, DataReaderContainer dataReader, Int32 schemaCount, DataColumn parentChapterColumn, Object parentChapterValue)
在 System.Data.Common.DataAdapter.FillMapping(DataSet dataset, DataTable datatable, String srcTable, DataReaderContainer dataReader, Int32 schemaCount, DataColumn parentChapterColumn, Object parentChapterValue)
在 System.Data.Common.DataAdapter.FillFromReader(DataSet dataset, DataTable datatable, String srcTable, DataReaderContainer dataReader, Int32 startRecord, Int32 maxRecords, DataColumn parentChapterColumn, Object parentChapterValue)
在 System.Data.Common.DataAdapter.Fill(DataTable[] dataTables, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)
在 System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
在 System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior)
在 System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)
在 WindowsFormsApplication1.Form1.txtLot_KeyPress(Object sender, KeyPressEventArgs e) 位置 c:\Users\1510014\Desktop\副本\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs:行号 34
在 System.Windows.Forms.Control.OnKeyPress(KeyPressEventArgs e)
在 System.Windows.Forms.Control.ProcessKeyEventArgs(Message& m)
在 System.Windows.Forms.Control.ProcessKeyMessage(Message& m)
在 System.Windows.Forms.Control.WmKeyChar(Message& m)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.TextBoxBase.WndProc(Message& m)
在 System.Windows.Forms.TextBox.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.Run(Form mainForm)
在 WindowsFormsApplication1.Program.Main() 位置 c:\Users\1510014\Desktop\副本\WindowsFormsApplication1\WindowsFormsApplication1\Program.cs:行号 18
InnerException:
code]
...全文
484 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
sliang28 2016-02-04
  • 打赏
  • 举报
回复
引用 10 楼 ajianchina 的回复:
感觉问题应该是在网络管理及驱动层面,你可以先更新一下驱动,在主板厂家官网下载驱动后试试。
问题已经被我解决. 解决方案:我用了PostGres官方的Npgsql替代了ODBC,问题完美解决. 虽然问题没了,终究没有找到为什么,不过谢谢大家了.
ajianchina 2016-02-03
  • 打赏
  • 举报
回复
Framework是什么版本,记得微软发布过过一个framework版本,我查了一下版本号是4.5.2,其中包括修复此bug。 同时有时客户机安装的一些软件,如迅雷,会修改windows网络编程接口winsock,也会导致此现象出现,这也是很多遇到你这种问题的人所发现的原因,但他们不是像你在第一、二次运行没有问题,而是根本就无法连接,直接提示跟你同样的错误,你也可以先修复一下winsock看看。cmd > netsh winsock reset > 重启
sliang28 2016-02-03
  • 打赏
  • 举报
回复
引用
加个try catch打个日志出来,简化后应该不会有问题,先确定是不是这段简化后代码的问题


现在这个工程里面什么也没有,只有这段代码。
xdashewan 2016-02-03
  • 打赏
  • 举报
回复
引用 5 楼 u012744089 的回复:
我那个输入为扫描枪输入,每输入一次,程序就运行一次,速度也不是很快。还是同样的问题。
加个try catch打个日志出来,简化后应该不会有问题,先确定是不是这段简化后代码的问题
sliang28 2016-02-03
  • 打赏
  • 举报
回复
引用
你的Conn只见close不见open啊,难道你认为cmd.Connection = Conn;就算是重新open了?KeyPress事件去多次抽取数据库,所有处理共用一个conn,当conn被关闭了肯定出问题
我把工程简化为下段代码:
引用
private void txtLot_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 13) { StringBuilder sql = new StringBuilder(); sql.Append("select w.workorder_id, w.part_no ,(rp.good_qty + rp.nogood_qty) as qty,rp.finish_timestamp from workorder as w inner join rosa_process_finish as rp on w.workorder_id = rp.workorder_id where w.workorder_id = 'RS8611369000001'"); OdbcConnection Conn = new OdbcConnection("Driver={PostgreSQL UNICODE};server=xxxxxxxxx;port=xxxx;database=xxxxxx;uid=xxx;pwd=xxx"); Conn.Open(); OdbcCommand cmd = new OdbcCommand(); cmd.CommandText = sql.ToString(); cmd.Connection = Conn; OdbcDataAdapter dr = new OdbcDataAdapter(cmd); DataTable dt = new DataTable(); dr.Fill(dt); cmd.Connection.Close(); txtLot.Text = string.Empty; } }
我那个输入为扫描枪输入,每输入一次,程序就运行一次,速度也不是很快。还是同样的问题。
sliang28 2016-02-03
  • 打赏
  • 举报
回复
引用
这段看不出什么问题 检查下第18行写了什么 数据源是数据库还是excel?
第十八行代码是application.run,这个没有什么.数据源为postgres.
  • 打赏
  • 举报
回复
是否为循环读写,导致链接或者资源没有及时释放!
xdashewan 2016-02-03
  • 打赏
  • 举报
回复
你的Conn只见close不见open啊,难道你认为cmd.Connection = Conn;就算是重新open了?KeyPress事件去多次抽取数据库,所有处理共用一个conn,当conn被关闭了肯定出问题
秋的红果实 2016-02-03
  • 打赏
  • 举报
回复
这段看不出什么问题 检查下第18行写了什么 数据源是数据库还是excel?
ajianchina 2016-02-03
  • 打赏
  • 举报
回复
感觉问题应该是在网络管理及驱动层面,你可以先更新一下驱动,在主板厂家官网下载驱动后试试。
sliang28 2016-02-03
  • 打赏
  • 举报
回复
引用 8 楼 ajianchina 的回复:
Framework是什么版本,记得微软发布过过一个framework版本,我查了一下版本号是4.5.2,其中包括修复此bug。 同时有时客户机安装的一些软件,如迅雷,会修改windows网络编程接口winsock,也会导致此现象出现,这也是很多遇到你这种问题的人所发现的原因,但他们不是像你在第一、二次运行没有问题,而是根本就无法连接,直接提示跟你同样的错误,你也可以先修复一下winsock看看。cmd > netsh winsock reset > 重启
在发帖之前这些方法我都试过了,还是同样的问题. 我们的电脑安装的东西都是一样的,我只不过多了VS而已. 奇怪了,怎么试都不行.

110,532

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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