我正在开发一个 C#.NET 应用程序,它连接到 MySQL DB 并使用一些不安全的代码来使用旧的 C++ dll。
程序一启动,就会加载dll并分配一个指针,然后初始化一些C#变量,然后删除指针。
在程序中,我有一个对话框,其中有一个选项卡控件。此“对话框”的构造函数从使用旧 dll 初始化的变量和数据库中获取数据,并从这里开始问题:
Dictionary<String, int> dizUM;
MySqlCommand myCom;
MySqlConnection myConn;
MySqlDataReader myRead;
cmbRigheCons.Items.Add( MosaicoS.Properties.Resources.STR_CONS_DESC_GEN );
cmbRigheCons.Items.Add( MosaicoS.Properties.Resources.STR_CONS_TIP_SOTT );
cmbRigheCons.Items.Add( MosaicoS.Properties.Resources.STR_CONS_SOTT );
cmbRigheCons.Items.Add( MosaicoS.Properties.Resources.STR_CONS_NOM_PEZ_CONS );
cmbRigheCons.SelectedIndex = MosaicoMain.CFG.DescCons;
cmbRigheVarMat.Items.Add( MosaicoS.Properties.Resources.STR_CONS_DESC_GEN );
cmbRigheVarMat.Items.Add( MosaicoS.Properties.Resources.STR_CONS_TIP_SOTT );
cmbRigheVarMat.Items.Add( MosaicoS.Properties.Resources.STR_CONS_SOTT );
cmbRigheVarMat.Items.Add( MosaicoS.Properties.Resources.STR_CONS_NOM_PEZ_CONS );
cmbRigheVarMat.Items.Add( MosaicoS.Properties.Resources.STR_CONS_NOM_PEZ_NEL_CONS );
cmbRigheVarMat.SelectedIndex = MosaicoMain.CFG.DescVarMat;
try
{
string connessione;
connessione = MosaicoMain.CFG.DBConnection.Replace( "Option=3;", "" );
connessione = connessione.Replace( "Driver={MySQL ODBC 5.1 Driver};", "" );
myConn = new MySqlConnection( connessione );
myCom = new MySqlCommand();
myConn.Open();
myCom.Connection = myConn;
myCom.CommandText = @"SELECT umi_id, umi_um FROM unimis WHERE umi_tipoum = 0 ORDER BY umi_um;";
myRead = myCom.ExecuteReader();
dizUM = new Dictionary<String, int>();
while( myRead.Read() )
dizUM.Add( myRead.GetString( 1 ), myRead.GetInt32( 0 ) );
cmbUMSup.DataSource = new BindingSource( dizUM, null );
cmbUMSup.DisplayMember = "Key";
cmbUMSup.ValueMember = "Value";
myRead.Close();
myConn.Close();
}
catch( MySqlException mySqlException )
{
MessageBox.Show( mySqlException.ToString() );
}
在这里,如果我从 myConn.Open(); 发表评论 到 myConn.Close(); 我没有遇到任何麻烦,但是当我在表之间切换时,打开连接会导致几乎立即的程序崩溃,但不是在程序的这一部分,而是在:
private void configurazioniToolStripMenuItem1_Click( object sender, EventArgs e )
{
try
{
frmConfig = new FrmConfigurazioni();
frmConfig.ShowDialog();
}
catch( Exception exception )
{
MessageBox.Show( exception.ToString() );
}
}
这是我加载前一个代码片段的地方。错误是:
Managed Debugging Assistant 'FatalExecutionEngineError' has detected a problem in
'C:\DEVELOP\MosaicoS\MosaicoS\bin\Debug\MosaicoS.vshost.exe'.
Additional Information: The runtime has encountered a fatal error.
The address of the error was at 0x792380d9, on thread 0x824.
The error code is 0xc0000005.
This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code.
Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.
这也不例外。如果我评论代码,我不会崩溃,而且,如果我尝试在另一个解决方案中使用相同的字符串连接到同一个数据库,我没有问题。我该如何解决这个问题?
提前致谢。