OdbcDataAdapter填充DataTable时出现“尝试读取或写入受保护的内存”
下面函数可以正常调用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]