问题描述
原VB代码段:PublicSubRsCnn()DimstrPathAsStringDimstrFileNameAsStringDimsqlStringAsStringDimcnnAsNewADODB.ConnectionDimrstAsNewADODB.Recordsetcnn.CursorLocation=adUseClientstrPath=App.Path&"datas"strFileName="staffslist.xls"Withcnn.Provider="Microsoft.Jet.OleDB.4.0".ConnectionString="ExtendedProperties='Excel8.0;Hdr=Yes;';DataSource="&strPath&strFileName.OpenEndWithsqlString="SelectID,Name,PhoneNumber,CodeFrom[Sheet1$]"Setrst=cnn.Execute(sqlString)SetFrmSetupStaff.DataGrid1.DataSource=rstrst.RequeryFrmSetupStaff.DataGrid1.RefreshFrmSetupStaff.txtRecordCount.Text=rst.RecordCountFrmSetupStaff.txtRecordCount.RefreshEndSub改写的vb.net代码PrivateSubForm1_Load(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesMyBase.LoadDimstrPathAsStringDimstrFileNameAsStringDimsqlStringAsStringDimcnnAsNewADODB.ConnectionDimrstAsNewADODB.Recordsetcnn.CursorLocation=ADODB.CursorLocationEnum.adUseClientstrPath=Application.StartupPath&""strFileName="staffslist.xls"Withcnn.Provider="Microsoft.Jet.OleDB.4.0".ConnectionString="ExtendedProperties='Excel8.0;Hdr=Yes;';DataSource="&strPath&strFileName.Open()EndWithsqlString="SelectID,Name,PhoneNumber,CodeFrom[Sheet1$]"rst=cnn.Execute(sqlString)Me.DataGridView1.DataSource=rstrst.Requery()Me.DataGridView1.Refresh()EndSub改写后无显示效果,也未报错。VB6.0的代码能正确显示。要抓狂了,看过好多教材,没找到问题在哪里。还请高手指教。
解决方案
解决方案二:
'引入命名空间ImportsSystem.Data.OleDbTryDimApppathAsString=Application.StartupPath'获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称DimDatabasePathAsString=Apppath&"MyTest.mdb;"'数据库文件总路径名DimConnStringAsString="Provider=Microsoft.Jet.OLEDB.4.0;"&"DataSource="&DatabasePath&_"PersistSecurityInfo=False"'连接字符串'DimConnStringAsString="Provider=Microsoft.Jet.OLEDB.4.0;DataSource=D:AccessDB1binDebugMyTest.mdb;;PersistSecurityInfo=False"DimSqlStrAsString="select*FROMTestInfowhere1=1"IfNotString.IsNullOrEmpty(Trim(Me.txtName.Text))Then'带条件查询SqlStr&="andXHlike'%"&Trim(Me.txtName.Text)&"%'"EndIfDimOleDbConnAsOleDbConnection=NewOleDbConnection(ConnString)OleDbConn.Open()DimMyDataSetAsDataSet=NewDataSet()DimMyOleDataAdapterAsOleDbDataAdapter=NewOleDbDataAdapter()MyOleDataAdapter.SelectCommand=NewOleDbCommand(SqlStr,OleDbConn)MyOleDataAdapter.Fill(MyDataSet)Me.Dg.DataSource=MyDataSet.Tables(0)'绑定DataGrid数据源MyOleDataAdapter.Dispose()MyDataSet.Dispose()OleDbConn.Close()OleDbConn.Dispose()CatchexAsExceptionMessageBox.Show(ex.ToString)EndTry
供参考!
解决方案三:
没试过,应该是DataGridView控件不支持ado里的结果集