如果樓主熟悉VB6,可以直接在項(xiàng)目中添加ADODB的Com引用,這樣你就可以像VB6那樣操作數(shù)據(jù)庫(kù)了!
網(wǎng)站設(shè)計(jì)制作過程拒絕使用模板建站;使用PHP+MYSQL原生開發(fā)可交付網(wǎng)站源代碼;符合網(wǎng)站優(yōu)化排名的后臺(tái)管理系統(tǒng);網(wǎng)站設(shè)計(jì)、網(wǎng)站制作收費(fèi)合理;免費(fèi)進(jìn)行網(wǎng)站備案等企業(yè)網(wǎng)站建設(shè)一條龍服務(wù).我們是一家持續(xù)穩(wěn)定運(yùn)營(yíng)了10余年的創(chuàng)新互聯(lián)建站網(wǎng)站建設(shè)公司。
另外
.NET
Framework中連接數(shù)據(jù)庫(kù)要用到ADO.NET。如果培嫌要操作Access數(shù)據(jù)庫(kù),要用到System.Data.OleDb命名空間下的許多類。
比如按樓主所說,“配舉手我想在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)閉連接
參考一下下耐桐面這段代碼:
Imports System.Data.OleDb
Public Class Form1
Dim SqlCOM As OleDbConnection
Dim SqlUse As OleDbDataAdapter
Dim Dt As DataSet
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
'連接數(shù)據(jù)庫(kù)
SqlCOM = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\LP\Documents\Database1.accdb")
'執(zhí)行數(shù)據(jù)命令獲取表
SqlUse = New OleDbDataAdapter("SELECT * FROM 表1", SqlCOM)
Dt = New DataSet
SqlUse.Fill(Dt) '放入dataset中
Catch
MsgBox(Err.Description, 48)
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
DataGridView1.DataSource = Dt.Tables(0).DefaultView 'DVG與猛廳DADASET同步數(shù)據(jù)顯示
Catch
MsgBox(Err.Description, 32)
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
Dim ass As New OleDbCommandBuilder(SqlUse) '關(guān)聯(lián)DataSet和數(shù)據(jù)庫(kù)的操作(更新)
SqlUse.UpdateCommand = ass.GetUpdateCommand() '執(zhí)行更新所需的 OleDbCommand 對(duì)象
SqlUse.Update(Dt.Tables(0)) '昌知坦執(zhí)行OleDbDataAdapter的Update更新數(shù)據(jù)庫(kù)
Catch
MsgBox(Err.Description, 32)
Finally
MsgBox("更新成功", 64)
End Try
End Sub
End Class
使用向?qū)гO(shè)置數(shù)據(jù)庫(kù)連接的好處是省去寫代碼的過程,弊端是不易更改連接
使用代碼連接的好處是需要編寫代碼,但容易更改和維護(hù)
如:數(shù)據(jù)庫(kù)的位置變化,直接更改代碼顯然容易一些
代碼:
imports System.data.oledb /*引用命名空間*/
dim cnstr = "provider=microsoft.jet.oledb.4.0,data source=Access數(shù)據(jù)庫(kù)文件位置" /*定義連接字符串枯老*/
dim cn as oledbconnection = new oledbconnection(cnstr) /*定義連接*/
cn.open() /*打開連接*/
sqlstr="御敗備select... from... 或其他sql"
dim cmd as sqlcommand=new sqlcommand()
cmd點(diǎn)抗 mandtext=sqlstr
cmd.connection=cn
/*以上是建立執(zhí)行語(yǔ)句*/
cmd.executenonquery() 不返回結(jié)果
cmd.executescalar() 返回單個(gè)結(jié)果
cmd.executereader() 返回結(jié)果集
/*以上是執(zhí)行sql語(yǔ)句的方法*/
cn.close() /*關(guān)閉連接*/
你要做用戶驗(yàn)證的話,先根據(jù)用戶名查詢對(duì)應(yīng)的密碼,再判斷用戶輸入鎮(zhèn)毀的密碼和查詢結(jié)果是不是相同就可以了
用cmd.executescalar() 這個(gè)方法
客戶端簡(jiǎn)單代碼:
Imports System.Net.Sockets
Imports System.IO
Private Output As NetworkStream
Private Writer As BinaryWriter
Private Reader As BinaryReader
Dim StrSend As String
Dim client As New TcpClient '
client.Connect(servIP, 2006)
Output = client.GetStream
Writer = New BinaryWriter(Output)
Reader = New BinaryReader(Output)
StrSend = "字符串"
System.Threading.Thread.Sleep(100)
Writer.Write(StrSend)
Try
Message = Reader.ReadString
If Message = "OK" Then
LBinfo.Text = "成功!" '測(cè)試
Else
LBinfo.Text = "失??!"
End If
System.Threading.Thread.Sleep(100)
Catch ex As Exception
MessageBox.Show("Client Application Closing!")
Finally
Writer.Close()
Reader.Close()
Output.Close()
client.Close()
End Try
服務(wù)器端
用線程
Imports System.Net.Sockets
Imports System.IO
Imports System.Threading
Imports System.Windows.Forms
Private Connection As Socket
Private readThread As Thread
Private SocketStream As NetworkStream
Private Writer As BinaryWriter
Private Reader As BinaryReader
Public Sub New()
MyBase.New()
'該調(diào)用是 Windows 窗體設(shè)計(jì)器所必需的。
InitializeComponent()
'者裂在 InitializeComponent() 調(diào)用之后首梁閉添加任渣雀何初始化
readThread = New Thread(AddressOf RunServer)
readThread.Start()
End Sub
Public Sub RunServer()
Dim Listener As TcpListener
Try
Listener = New TcpListener(Net.IPAddress.Parse("192.168.1.16"), 2006)
Listener.Start()
While True
Connection = Listener.AcceptSocket 'accept an incoming connection
SocketStream = New NetworkStream(Connection) 'create networkstream object associated with socket
Writer = New BinaryWriter(SocketStream) 'create object for transferring data across stream
Reader = New BinaryReader(SocketStream) 'read string data sent from client
Try
Do
strIncept = Reader.ReadString
strIncept = DataDisp(strIncept) '此處插入數(shù)據(jù)處理程序()
SendInfoToClient(strIncept) 'send to client
Loop While Connection.Connected
Catch ex As Exception
MsgBox(ex.ToString)
Finally
Writer.Close()
Reader.Close()
SocketStream.Close()
End Try
End While
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Public Sub SendInfoToClient(ByVal Info As String)
'Send to client
Try
If Connection.Connected Then
Writer.Write(Info)
Connection.Close()
End If
Catch ex As SocketException
MessageBox.Show(ex.ToString)
End Try
End Sub
整個(gè)過程都有了