Public Function GetDataTable(ByVal strSql As String) As DataTable
'实例一个DataTable对象
Dim myDataTable As New DataTable
Try
'实例一个OledbDataAdapter对象,并指定其OleDbCommand对象,下面这两种是一样的。
Dim myDataAdapter As New OleDbDataAdapter(strSql, strConnection)
'Dim myDataAdapter As New OleDbDataAdapter(New OleDbCommand(strSql, myConnection))
'填充上面实例的DataTable对象
myDataAdapter.Fill(myDataTable)
Catch ex As Exception
'提示错误
MessageBox.Show(ex.Message, "警告!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Finally
'关闭连接
If myConnection.State = ConnectionState.Open Then
myConnection.Close()
End If
End Try
'函数返回值,一个DataTable对象
Return myDataTable
End Function
dim intSum as integer=GetDataTable(strSql).columns.count