0

我有一个将 Vb6 迁移到 c#.net 的项目。当我运行这个 c# 解决方案时,我得到了一个 InvalidCastException,例如Unable to cast object of type 'ADODB.Recordset' to type 'MSDATASRC.DataSource'

在语句DataGrid1.DataSource = (MSDATASRC.DataSource)(object)(Module1.rs); .

其中 rs 生成如下: public static ADODB.Connection conn = null; 公共静态 ADODB.Recordset rs = null;

     conn = new ADODB.Connection();
     conn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.Windows.Forms.Application.StartupPath + "\\myDatabase.mdb;Persist Security Info=False",String.Empty,String.Empty,-1);

     rs = new ADODB.Recordset();         
     rs.ActiveConnection = conn;
     rs.CursorLocation = (ADODB.CursorLocationEnum)(MSAdodcLib.CursorLocationEnum.adUseClient);
     rs.CursorType = (ADODB.CursorTypeEnum)(MSAdodcLib.CursorTypeEnum.adOpenDynamic);
     rs.LockType = ADODB.LockTypeEnum.adLockOptimistic;
     rs.Source = "SELECT * FROM MyTable";

     rs.Open(null,null,ADODB.CursorTypeEnum.adOpenUnspecified,ADODB.LockTypeEnum.adLockUnspecified,-1);

任何人都可以请你帮我解决这个问题。

4

1 回答 1

0

尝试:

 DataGrid1.DataSource = DataGrid1.DataSource = (MSDATASRC.DataSource)(object)(Module1)
于 2012-12-10T08:22:43.960 回答