如果楼主熟悉VB6,可以直接在项目中添加ADODB的Com引用,这样你就可以像VB6那样操作数据库了!
网站设计制作过程拒绝使用模板建站;使用PHP+MYSQL原生开发可交付网站源代码;符合网站优化排名的后台管理系统;网站设计、网站制作收费合理;免费进行网站备案等企业网站建设一条龙服务.我们是一家持续稳定运营了10余年的创新互联建站网站建设公司。
另外
.NET
Framework中连接数据库要用到ADO.NET。如果培嫌要操作Access数据库,要用到System.Data.OleDb命名空间下的许多类。
比如按楼主所说,“配举手我想在textbox1中显示表一中【一些数据】字段下的第一个内容”:
'首先导入命名空间
Imports
System.Data
Imports
System.Data.OleDb
'然后在某一个事件处理程序中写:
Dim
conn
As
New
OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=数据库.accdb;Jet
OLEDB:Database
Password=MyDbPassword")
Dim
command
As
New
OleDbCommand("Select
*
From
数据表",
conn)
conn.Open()
'打开数据库连接
Dim
reader
As
OleDbDataReader
=
command.ExecuteReader()
'执行SQL语句,返回OleDbDataReader
对象
Do
While
reader.Read()
'读取答前一条数据
textbox1.Text
+=
reader("一些数据")
VbCrLf
Loop
reader.Close()
'关闭OleDbDataReader
conn.Close()
'关闭连接
参考一下下耐桐面这段代码:
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
'连接数据库
SqlCOM = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\LP\Documents\Database1.accdb")
'执行数据命令获取表
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同步数据显示
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) '关联DataSet和数据库的操作(更新)
SqlUse.UpdateCommand = ass.GetUpdateCommand() '执行更新所需的 OleDbCommand 对象
SqlUse.Update(Dt.Tables(0)) '昌知坦执行OleDbDataAdapter的Update更新数据库
Catch
MsgBox(Err.Description, 32)
Finally
MsgBox("更新成功", 64)
End Try
End Sub
End Class
使用向导设置数据库连接的好处是省去写代码的过程,弊端是不易更改连接
使用代码连接的好处是需要编写代码,但容易更改和维护
如:数据库的位置变化,直接更改代码显然容易一些
代码:
imports System.data.oledb /*引用命名空间*/
dim cnstr = "provider=microsoft.jet.oledb.4.0,data source=Access数据库文件位置" /*定义连接字符串枯老*/
dim cn as oledbconnection = new oledbconnection(cnstr) /*定义连接*/
cn.open() /*打开连接*/
sqlstr="御败备select... from... 或其他sql"
dim cmd as sqlcommand=new sqlcommand()
cmd点抗 mandtext=sqlstr
cmd.connection=cn
/*以上是建立执行语句*/
cmd.executenonquery() 不返回结果
cmd.executescalar() 返回单个结果
cmd.executereader() 返回结果集
/*以上是执行sql语句的方法*/
cn.close() /*关闭连接*/
你要做用户验证的话,先根据用户名查询对应的密码,再判断用户输入镇毁的密码和查询结果是不是相同就可以了
用cmd.executescalar() 这个方法
客户端简单代码:
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 = "成功!" '测试
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
服务器端
用线程
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()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'者裂在 InitializeComponent() 调用之后首梁闭添加任渣雀何初始化
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) '此处插入数据处理程序()
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
整个过程都有了