重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
由于你用的是access数据库,access数据库是本地数据库,不支持网络使用.建议你改用sql server或者其它支持网络应用的数据库.
特克斯ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:13518219792(备注:SSL证书合作)期待与您的合作!
如果你要在另一台机器上使用access数据库,一是你把数据库拷贝到另一台机器上,但这样是两个数据库是相互独立的,数据不能共享.另一种方法是开一个有权限的网络共享,远程计算机通过网络共享的方法访问你所使用的access数据库,两种方法只要用odbc连接成功,连接字符串和上面使用的是一样的写法
参考一下下面这段代码就可以了。
Imports System.Data
'引入数据库操作类命名空间
Imports System.Data.OleDb
'引入ADO.NET操作命名空间
Public Class FrmModifystInfo
Inherits System.Windows.Forms.Form
Public ADOcmd As OleDbDataAdapter
Public ds As DataSet = New DataSet()
'建立DataSet对象
Public mytable As Data.DataTable
'建立表单对象
Public myrow As Data.DataRow
'建立数据行对象
Public rownumber As Integer
'定义一个整型变量来存放当前行数
Public SearchSQL As String
Public cmd As OleDbCommandBuilder
'======================================================
#Region " Windows 窗体设计器生成的代码 "
#End Region
'======================================================
Private Sub FrmModifystInfo_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
'窗体的载入
TxtSID.Enabled = False
TxtName.Enabled = False
ComboSex.Enabled = False
TxtBornDate.Enabled = False
TxtClassno.Enabled = False
TxtRuDate.Enabled = False
TxtTel.Enabled = False
TxtAddress.Enabled = False
TxtComment.Enabled = False '设置信息为只读
Dim tablename As String = "student_Info "
SearchSQL = "select * from student_Info "
ExecuteSQL(SearchSQL, tablename) '打开数据库
ShowData() '显示记录
End Sub
Private Sub ShowData()
'在窗口中的textbox中显示数据
myrow = mytable.Rows.Item(rownumber)
TxtSID.Text = myrow.Item(0).ToString
TxtName.Text = myrow.Item(1).ToString
ComboSex.Text = myrow.Item(2).ToString
TxtBornDate.Text = Format(myrow.Item(3), "yyyy-MM-dd ")
TxtClassno.Text = myrow.Item(4).ToString
TxtTel.Text = myrow.Item(5).ToString
TxtRuDate.Text = Format(CDate(myrow.Item(6)), "yyyy-MM-dd ")
TxtAddress.Text = myrow.Item(7).ToString
TxtComment.Text = myrow.Item(8).ToString
End Sub
Private Sub BtFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtFirst.Click
'指向第一条数据
rownumber = 0
ShowData()
End Sub
Private Sub BtPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtPrev.Click
'指向上一条数据
BtNext.Enabled = True
rownumber = rownumber - 1
If rownumber 0 Then
rownumber = 0 '如果到达记录的首部,行号设为零
BtPrev.Enabled = False
End If
ShowData()
End Sub
Private Sub BtNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtNext.Click
'指向上一条数据
BtPrev.Enabled = True
rownumber = rownumber + 1
If rownumber mytable.Rows.Count - 1 Then
rownumber = mytable.Rows.Count - 1 '判断是否到达最后一条数据
BtNext.Enabled = False
End If
ShowData()
End Sub
Private Sub BtLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtLast.Click
'指向最后一条数据
rownumber = mytable.Rows.Count - 1
ShowData()
End Sub
Private Sub BtDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtDelete.Click
mytable.Rows.Item(rownumber).Delete() '删除记录
If MsgBox( "确定要删除改记录吗? ", MsgBoxStyle.OKCancel + vbExclamation, "警告 ") = MsgBoxResult.OK Then
cmd = New OleDbCommandBuilder(ADOcmd)
'使用自动生成的SQL语句
ADOcmd.Update(ds, "student_Info ")
BtNext.PerformClick()
End If
End Sub
Private Sub BtModify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtModify.Click
TxtSID.Enabled = False '关键字段只读
TxtName.Enabled = True '可读写
ComboSex.Enabled = True
TxtBornDate.Enabled = True
TxtClassno.Enabled = True
TxtRuDate.Enabled = True
TxtTel.Enabled = True
TxtAddress.Enabled = True
TxtComment.Enabled = True
End Sub
Private Sub BtUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtUpdate.Click
If Not Testtxt(TxtName.Text) Then
MsgBox( "请输入姓名! ", vbOKOnly + vbExclamation, "警告 ")
TxtName.Focus()
Exit Sub
End If
If Not Testtxt(ComboSex.Text) Then
MsgBox( "请选择性别! ", vbOKOnly + vbExclamation, "警告 ")
ComboSex.Focus()
Exit Sub
End If
If Not Testtxt(TxtClassno.Text) Then
MsgBox( "请选择班号! ", vbOKOnly + vbExclamation, "警告 ")
TxtClassno.Focus()
Exit Sub
End If
If Not Testtxt(TxtTel.Text) Then
MsgBox( "请输入联系电话! ", vbOKOnly + vbExclamation, "警告 ")
TxtTel.Focus()
Exit Sub
End If
If Not Testtxt(TxtAddress.Text) Then
MsgBox( "请输入家庭住址! ", vbOKOnly + vbExclamation, "警告 ")
TxtAddress.Focus()
Exit Sub
End If
If Not IsNumeric(Trim(TxtSID.Text)) Then
MsgBox( "请输入数字学号! ", vbOKOnly + vbExclamation, "警告 ")
Exit Sub
TxtSID.Focus()
End If
If Not IsDate(TxtBornDate.Text) Then
MsgBox( "出生时间应输入日期格式(yyyy-mm-dd)! ", vbOKOnly + vbExclamation, "警告 ")
Exit Sub
TxtBornDate.Focus()
End If
If Not IsDate(TxtRuDate.Text) Then
MsgBox( "入校时间应输入日期格式(yyyy-mm-dd)! ", vbOKOnly + vbExclamation, "警告 ")
TxtRuDate.Focus()
Exit Sub
End If
myrow.Item(0) = Trim(TxtSID.Text)
myrow.Item(1) = Trim(TxtName.Text)
myrow.Item(2) = Trim(ComboSex.Text)
myrow.Item(3) = Trim(TxtBornDate.Text)
myrow.Item(4) = Trim(TxtClassno.Text)
myrow.Item(5) = Trim(TxtTel.Text)
myrow.Item(6) = Trim(TxtRuDate.Text)
myrow.Item(7) = Trim(TxtAddress.Text)
myrow.Item(8) = Trim(TxtComment.Text)
mytable.GetChanges()
cmd = New OleDbCommandBuilder(ADOcmd)
'使用自动生成的SQL语句
ADOcmd.Update(ds, "student_Info ")
'对数据库进行更新
MsgBox( "修改学籍信息成功! ", vbOKOnly + vbExclamation, "警告 ")
TxtName.Enabled = False
ComboSex.Enabled = False
TxtBornDate.Enabled = False
TxtClassno.Enabled = False
TxtRuDate.Enabled = False
TxtTel.Enabled = False
TxtAddress.Enabled = False
TxtComment.Enabled = False '重新设置信息为只读
End Sub
Private Sub BtCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtCancel.Click
TxtSID.Enabled = False
TxtName.Enabled = False
ComboSex.Enabled = False
TxtBornDate.Enabled = False
TxtClassno.Enabled = False
TxtRuDate.Enabled = False
TxtTel.Enabled = False
TxtAddress.Enabled = False
TxtComment.Enabled = False
End Sub
Public Function ExecuteSQL(ByVal SQL As String, ByVal table As String)
Try
'建立ADODataSetCommand对象
'数据库查询函数
ADOcmd = New OleDbDataAdapter(SQL, "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\student.mdb ")
'建立ADODataSetCommand对象
ADOcmd.Fill(ds, table) '取得表单
mytable = ds.Tables.Item(0) '取得名为table的表
rownumber = 0 '设置为第一行
myrow = mytable.Rows.Item(rownumber)
'取得第一行数据
Catch
MsgBox(Err.Description)
End Try
End Function
End Class
请采纳。
如果要程序直接连接DB2数据库,必须要有连接数据库的驱动程序,连接的代码,你可以参考如下(以前用C#写的,你改成VB.NET即可,ODBC连接对象)
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;
}
如果你是开发桌面程序而又不想在客户端安装数据库的驱动程序,那么你可以采用web网站(含webservice)或者remoting方式,只需要在服务端安装驱动即可
极其简单……
Public ConnStr As String = "Dsn=WFReport;uid=cac_reader;pwd=cac_reader"
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ds As New DataSet
Dim conn As OdbcConnection
Dim da As OdbcDataAdapter
conn = New OdbcConnection
conn.ConnectionString = ConnStr
conn.Open()
Dim strsql As String = "SELECT * from A"
da = New OdbcDataAdapter(strsql, conn)
Dim strDSPaking As String = "DSPaking"
da.Fill(ds, strDSPaking)
End Sub
这个问题好宽泛,简单说下步骤吧:
简单来说,直接连接需要使用SQLConnection、SQLDataAdapter、SQLCommand、Datatable四个控件,先写好连接字符串,然后建立连接,写好SQL语句,通过DataAdapter从数据库读取数据到Datatable。这样只有在SQL服务器中附加数据库才能实现访问。还可以使用DSN实现数据库连接,控件依次更换为ODBCConnection、ODBCDataAdapter、ODBCCommand、datatable、基本步骤和上面说的一样。
SQL语句就是从数据库查用户名对应的密码,将数据从数据库读取到DataTable后,与用户输入的密码比对,相符则登陆成功,否则提示错误。
过程中的细节问题欢迎追问
1、 用The SQL Server .NET Data Provider连接数据库
The SQL Server .NET Data Provider是利用SqlConnection类来连接SQL Server7.0或更高版本的数据库,
SqlConnection类位于名称空间System.Data.SqlClient下。
连接代码:
Dim sqlConnection1 As SqlClient.SqlConnection
Dim strConnect As String=”data source=服务器名;initial catalog=数据库名;user id=sa;password=;”
sqlConnection1=New System.Data.SqlClient.SqlConnection(strConnect)
sqlConnection1.open ‘打开数据库
sqlConnection1.close ‘关闭连接,释放资源
2、 用The OLE DB .NET Data Provider连接数据库
上面已经说过,利用The OLE DB .NET Data Provider可以访问Access、Oracle和SQL Server等种数据
库,那么,它是怎样访问这些数据库的呢?The OLE DB .NET Data Provider是通过位于名称空间Sy
stem.Data.OleDb类库下的OleDbConnection类来连接这三种不同类型的数据库的。下面举例说明:
1)连接SQL Server数据库
Dim oleDbConnection1 As OleDb.OleDbConnection
Dim strConnect As Sting=”Provider=SQLOLEDB;Persist Security Info=False;Data Source=服务器名;Initial Catalog=数据库名;User ID=sa;Password=;”
oleDbConnection1=New System.Data.OleDb.OleDbConnection(strConnect)
2)连接Access数据库
假设要连接的Access数据库名为“Example.mdb”,存放在d:\Data\目录下。
Dim oleDbConnection1 As OleDb.OleDbConnection
Dim strConnect As Sting=”Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\Data\ Example.mdb”
oleDbConnection1= New System.Data.OleDb.OleDbConnection(strConnect)
3)连接Oracle数据库
Dim oleDbConnection1 As OleDb.OleDbConnection
Dim strConnect As Sting=”Provider=MSDAORA;Data Source=服务器名;User ID=用户ID;Password=密码;”
oleDbConnection1= New System.Data.OleDb.OleDbConnection(strConnect)
3、 用The ODBC .NET Data Provider连接数据库
The ODBC .NET Data Provider连接数据库是通过OdbcConnection类来实现的,这个类位于名称空间
Microsoft.Data.Odbc下,而名称空间Microsoft.Data.Odbc是封装在Microsoft.Data.Odbc.dll文件下的。
由于篇幅有限,这里就只介绍连接Sql Server和Oracle数据库的方法,其他数据库的连接方法基本类
似,我就不再多讲了。
1)连接Sql Server数据库
Dim odbcDbConnetion1 As Microsoft.Data.OdbcConnection
Dim strConnect As Sting=”Driver={SQL Server};Server=服务器名;Uid=sa;pwd=;Database= 数据库名;”
odbcDbConnetion1=New Microsoft.Data.OdbcConnection(strConnect)
2)连接Oracle数据库
Dim odbcDbConnetion1 As Microsoft.Data.OdbcConnection
Dim strConnect As Sting=”Driver={Microsoft ODBC for Oracle};Server=服务器名;Uid=sa;pwd=;”
odbcDbConnetion1=New Microsoft.Data.OdbcConnection(strConnect)
四、总结
通过本文的介绍,读者基本掌握了在Visual Basic.NET中用ADO.NET和ODBC.NET连接各种数据库的方法
。以上三种驱动针对不同的数据库,它们的性能方面也有很大的不同:The SQL Server .NET Data Provider
的效率最高;The OLE DB .NET Data Provider的效率比较底;The ODBC .NET Data Provider的效率最慢。
具体连接哪一种数据库选用哪一种数据驱动要从工作效率方面来考虑。
以上回答你满意么?