如果要程序直接連接DB2數(shù)據(jù)庫(kù),必須要有連接數(shù)據(jù)庫(kù)的驅(qū)動(dòng)程序,連接的代碼,你可以參考如下(以前用C#寫的,你改成VB.NET即可,ODBC連接對(duì)象)
成都創(chuàng)新互聯(lián)公司服務(wù)項(xiàng)目包括渭源網(wǎng)站建設(shè)、渭源網(wǎng)站制作、渭源網(wǎng)頁(yè)制作以及渭源網(wǎng)絡(luò)營(yíng)銷策劃等。多年來(lái),我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,渭源網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到渭源省份的部分城市,未來(lái)相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
public?OdbcConnection?Db2Conn()
{
string?str?=?"DRIVER=IBM?DB2?ODBC?DRIVER;UID=db2admin;PWD=db2admin;AUTHENTICATION=SERVER;PORT=50000;HOSTNAME=192.168.100.98;PROTOCOL=TCPIP;DATABASE=YCDATA";
OdbcConnection?Conn?=?new?OdbcConnection(str);
return?Conn;
}
如果你是開發(fā)桌面程序而又不想在客戶端安裝數(shù)據(jù)庫(kù)的驅(qū)動(dòng)程序,那么你可以采用web網(wǎng)站(含webservice)或者remoting方式,只需要在服務(wù)端安裝驅(qū)動(dòng)即可
mports System.Drawing
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Data.OleDb
Imports System.Data
Public Class Form1
Inherits Form
Private components As System.ComponentModel.Container = Nothing
Private WithEvents lastrec As Button
Private WithEvents nextrec As Button
Private WithEvents previousrec As Button
Private WithEvents firstrec As Button
Private t_books As TextBox
Private t_nl As TextBox
Private t_xb As TextBox
Private t_xm As TextBox
Private t_id As TextBox
Private l_books As Label
Private l_nl As Label
Private l_xb As Label
Private l_xm As Label
Private l_id As Label
Private label1 As Label
Private myDataSet As DataSet
Private WithEvents button1 As Button
Private WithEvents button2 As Button
Private WithEvents button3 As Button
Private WithEvents button4 As Button
Private myBind As BindingManagerBase
Public Sub New ( )
MyBase.New ( )
GetConnected ( )
InitializeComponent ( )
End Sub
'清除在程序中使用過(guò)的資源
Protected Overloads Overrides Sub Dispose (ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose ( )
End If
End If
MyBase.Dispose ( disposing )
End Sub
Public Sub GetConnected ( )
'創(chuàng)建一個(gè)數(shù)據(jù)連接
Dim strCon As String = " Provider = Microsoft.Jet.OLEDB.4.0 ;
Data Source = db.mdb "
Dim myConn As OleDbConnection = New OleDbConnection ( )
myConn.ConnectionString = strCon
Dim strCom As String = " SELECT * FROM person "
'創(chuàng)建一個(gè) DataSet
myDataSet = New DataSet ( )
myConn.Open ( )
'通過(guò)OleDbDataAdapter對(duì)象得到一個(gè)數(shù)據(jù)集
Dim myCommand As OleDbDataAdapter = New OleDbDataAdapter
( strCom , myConn )
'把Dataset綁定books數(shù)據(jù)表
myCommand.Fill ( myDataSet , "person" )
'關(guān)閉此數(shù)據(jù)連接
myConn.Close ( )
End Sub
'插入數(shù)據(jù)記錄操作代碼
Private Sub button2_Click (ByVal sender As Object , _
ByVal e As System.EventArgs) Handles button2.Click
'判斷所有字段是否添完,添完則執(zhí)行,反之彈出提示
If ( t_id.Text "" And t_xm.Text ""
And t_xb.Text "" And t_nl.Text ""
And t_books.Text "" ) Then
Dim myConn1 As String = " Provider = Microsoft.Jet.OLEDB.4.0 ;
Data Source = db.mdb"
Dim myConn As OleDbConnection = New OleDbConnection ( myConn1 )
myConn.Open ( )
Dim strInsert As String = " INSERT INTO person ( id , xm , xb , nl
, zip ) VALUES ( " _
t_id.Text + " , '" _
t_xm.Text + "' , '" _
t_xb.Text + "' , " _
t_nl.Text + " , " _
t_books.Text + ")"
Dim inst As OleDbCommand = New OleDbCommand ( strInsert , myConn )
inst.ExecuteNonQuery ( )
myConn.Close ( )
myDataSet.Tables ( "person" ).Rows ( myBind.Position ).BeginEdit ( )
myDataSet.Tables ( "person" ).Rows ( myBind.Position ).EndEdit ( )
myDataSet.Tables ( "person" ).AcceptChanges ( )
Else
MessageBox.Show ( "必須填滿所有字段值!" , "錯(cuò)誤!" )
End If
End Sub
如果樓主熟悉VB6,可以直接在項(xiàng)目中添加ADODB的Com引用,這樣你就可以像VB6那樣操作數(shù)據(jù)庫(kù)了!
另外
.NET
Framework中連接數(shù)據(jù)庫(kù)要用到ADO.NET。如果要操作Access數(shù)據(jù)庫(kù),要用到System.Data.OleDb命名空間下的許多類。
比如按樓主所說(shuō),“我想在textbox1中顯示表一中【一些數(shù)據(jù)】字段下的第一個(gè)內(nèi)容”:
'首先導(dǎo)入命名空間
Imports
System.Data
Imports
System.Data.OleDb
'然后在某一個(gè)事件處理程序中寫:
Dim
conn
As
New
OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=數(shù)據(jù)庫(kù).accdb;Jet
OLEDB:Database
Password=MyDbPassword")
Dim
command
As
New
OleDbCommand("Select
*
From
數(shù)據(jù)表",
conn)
conn.Open()
'打開數(shù)據(jù)庫(kù)連接
Dim
reader
As
OleDbDataReader
=
command.ExecuteReader()
'執(zhí)行SQL語(yǔ)句,返回OleDbDataReader
對(duì)象
Do
While
reader.Read()
'讀取一條數(shù)據(jù)
textbox1.Text
+=
reader("一些數(shù)據(jù)")
VbCrLf
Loop
reader.Close()
'關(guān)閉OleDbDataReader
conn.Close()
'關(guān)閉連接