VB.Net DataSet 填充資料庫內容

'導入命名空間
Imports System.Data.OleDb


Public Class Form126
'定義變量
Dim ds As DataSet = New DataSet()
Dim i, cn As Integer
Dim Sql As String

'Oracle連線
Dim SqlTP As OleDbConnection
Dim CmdTP As New OleDbCommand
Dim drTP As OleDbDataReader
Dim AdapterTP As OleDbDataAdapter


'打开Oracle連線
Public Sub OpenAdapterTP()
SqlTP = New OleDbConnection("Provider=OraOLEDB.Oracle.1;Password=密碼;Persist Security Info=True;User ID=帳號;Data Source=資料庫")
If SqlTP.State = ConnectionState.Closed Then
SqlTP.Open()
End If

'填充到DataSet里
AdapterTP = New OleDbDataAdapter(Sql, SqlTP)
ds = New DataSet()
AdapterTP.Fill(ds, "p")

End Sub

'結束Oracle連線
Public Sub DisposeAdapterTP()
If SqlTP.State = ConnectionState.Closed Then
SqlTP.Dispose()
ElseIf SqlTP.State = ConnectionState.Open Then
SqlTP.Close()
SqlTP.Dispose()
Else
SqlTP.Dispose()
End If
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Sql = ("SELECT IMA01 FROM SH01.VW_DECA_IMA_STATE WHERE (IMA01 LIKE 'AMC100-01603%')")
OpenAdapterTP() '打开Oracle連線
TextBox1.Text = ds.Tables("p").Rows.Count '總筆數

If Not ds.Tables("p").Rows(0).Item(0) Is System.DBNull.Value Then '空值判斷
TextBox2.Text = ds.Tables("p").Rows(0).Item(0) '第1筆內容
End If

DisposeAdapterTP() '結束Oracle連線
End Sub


End Class

猜你喜欢

转载自www.cnblogs.com/leme-chen/p/9272385.html