我有一个迁移的Vb6到c#.net项目。当我运行这个c#解决方案时,我得到 InvalidCastException ,如无法将“ADODB.Recordset”类型的对象强制转换为“MSDATASRC.DataSource”
声明 DataGrid1.DataSource =(MSDATASRC.DataSource)(object)(Module1.rs); 。
其中rs生成如下: public static ADODB.Connection conn = null; public static 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);
任何人都可以帮我解决这个问题。
答案 0 :(得分:0)
尝试:
DataGrid1.DataSource = DataGrid1.DataSource = (MSDATASRC.DataSource)(object)(Module1)