'//
'// Name:用VB.Net读写数据库
'// Author:Q&f
'// Email:dengyu1230359@sina.com
'//
Imports System.Data.Odbc ' [/ Imports System.Data.OleDb/ Imports System.Data.SqlClient(三者在使用方法上基本上相同)]
'// 显示数据
Private Sub ShowData()
Dim Cnn As OdbcConnection
Dim Cmd As OdbcCommand
Dim Da As OdbcDataAdapter
Dim Ds As DataSet
Dim intIndex As Integer
'// 连接到数据库
Cnn = New OdbcConnection
Cnn.ConnectionString = "PROVIDER=MSDASQL;DRIVER={Microsoft Access Driver (*.MDB)};" & _
"DBQ=" & Server.MapPath("database/database.mdb") & ";UID=;PWD=;"
Cnn.Open()
Cmd = New OdbcCommand
Cmd.Connection = Cnn
Cmd.CommandType = CommandType.Text
Da = New OdbcDataAdapter
Ds = New DataSet
Da.SelectCommand = Cmd
'// 注意以下语句可以将不同SQL语句执行结果添加到同一个表中
Da.SelectCommand.CommandText = "SELECT ID,Title,Issuer,IssueDate,DepName AS Range " & _
"FROM IssueInforTable,DepartmentTable " & _
"WHERE IssueInforTable.DepID = DepartmentTable.DepID"
Da.Fill(Ds, "IssueInforTable")
Da.SelectCommand.CommandText = "SELECT ID,Title,Content,Issuer,IssueDate,Range " & _
"FROM IssueInforTable WHERE Range ='ALL'"
Da.Fill(Ds, "IssueInforTable")
If Ds.Tables("IssueInforTable").Rows.Count > 0 Then
DataGrid1.EditItemIndex = -1
DataGrid1.CurrentPageIndex = 0
DataGrid1.DataSource = Ds
DataGrid1.DataBind()
txtPage.Text = CStr(DataGrid1.CurrentPageIndex + 1)
lblInfor.Text = "记录总数:" & Ds.Tables("IssueInforTable").Rows.Count & _
"条.当前,第" & DataGrid1.CurrentPageIndex + 1 & "页,总计" & DataGrid1.PageCount & "页."
Else
CreateTempTable()
txtPage.Text = CStr(DataGrid1.CurrentPageIndex + 1)
lblInfor.Text = "没有找到任何相关记录"
End If
'// 保存数据集
Session("DataSet") = Ds
'// 保存记录总数
Session("RecordCount&