十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
mports System.Drawing
创新互联-专业网站定制、快速模板网站建设、高性价比清远网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式清远网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖清远地区。费用合理售后完善,十多年实体公司更值得信赖。
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
'清除在程序中使用过的资源
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 ( )
'创建一个数据连接
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 "
'创建一个 DataSet
myDataSet = New DataSet ( )
myConn.Open ( )
'通过OleDbDataAdapter对象得到一个数据集
Dim myCommand As OleDbDataAdapter = New OleDbDataAdapter
( strCom , myConn )
'把Dataset绑定books数据表
myCommand.Fill ( myDataSet , "person" )
'关闭此数据连接
myConn.Close ( )
End Sub
'插入数据记录操作代码
Private Sub button2_Click (ByVal sender As Object , _
ByVal e As System.EventArgs) Handles button2.Click
'判断所有字段是否添完,添完则执行,反之弹出提示
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 ( "必须填满所有字段值!" , "错误!" )
End If
End Sub
你这段代码里面有两个问题:
1.连接字符串不对,既然是在项目里面新建的数据库,那么在新建的时候它应该有一个数据源配置向导提示你创建一个数据集,这里面你可以不用它的数据集,但是你可以在这里获得连接字符串,比如,datasource不应该是localhost,而应该是 .\SQLEXPRESS,等等,字符串太长,我本机也没有安装开发工具, 你可以在 工具-连接到服务器里面调出 “服务器资源管理器”,这里可以添加一个数据连接,你可以在各连接的“属性”里面把连接字符串 拷贝出来。
2.既然是用command执行,那么你的connection肯定要先open,或者connect起来啊,这个你在你的con 里面打个“.”自动会有提示能看到的
做程序要多试,这样基础的问题不用上google,baidu,自己试就能试出来
建立数据集,绑定到窗口。不会的话,把要用的表从数据集拖到窗口,再删除就行了
提取方法
数据及名称.表格名称.rows(2).item(3).tostring 就是了
可用ado.net访问。
换电脑数据还在要求有服务器,要么是局域网内一台服务器,要么是公网服务器。
首先在项目的VB.NET界面,使用菜单【项目】--【添加引用】--【COM】
选择 Microsoft ADO Ext. 2.x for DDL and Security
然后单击【确定】,完成引用。
完整代码如下:
Imports ADOX
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'创建空的access数据库文件--数据库文件.mdb,密码为123
Dim Mycat As Catalog = New Catalog()
Mycat.Create("Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Engine Type=5;Data Source= 数据库文件.mdb;Jet OLEDB:Database Password=123")
'以下代码创建一个名为“实验数据表”
Dim MyTable As ADOX.Table = New ADOX.Table '定义新表
MyTable.Name = "实验数据表" '表命名
'给表“实验数据表” 创建一个字符串字段,字段名“姓名”
MyTable.Columns.Append("姓名", , ADOX.DataTypeEnum.adWChar)
'给表“实验数据表” 创建一个整数字段,字段名“学号”
MyTable.Columns.Append("学号", ADOX.DataTypeEnum.adInteger) '追加一个数字型字段
'给字段“学号”创建一个主键“PimaryKey_Field”
MyTable.Keys.Append("学号", ADOX.KeyTypeEnum.adKeyPrimary, "学号")
Mycat.Tables.Append(MyTable) '把所有的新字段追加到表
MyTable = Nothing
Mycat = Nothing
End Sub
End Class