vbnet创建数据库
我们需要引用ADO.具体的方法是,在"工程" -- "引用" 中,找到"Microsoft ActiveX Data Object *.* Library",这里的"*.*"是指的时ADO的版本号,一般来说,应用程序或ActiveX控件都具有向下兼容性,所以我们尽可能选择比较新的版本.以确保程序在能识别旧版本Access的同时,也能识别较新版本的Access.
然后我们需要在程序中创建一个对象.就好比我们在窗体上添加一个FileBox才能看到文件名一样,只有创建了ADO对象,我们才能够访问数据库.常用的对象有两个,Connection和Recordset.
创建这两个对象的具体方法是:
1.在引用后,使用New关键字,如
Private Conn As New ADODB.Connection
Private Reco As New ADODB.Recordset
2.在没引用时,用CreateObject创建对象:
Dim Conn,Reco
Set Conn = CreateObject("ADODB.Connection")
Set Reco = CreateObject("ADODB.Recordset")
创建了对象之后,下一步我们要做的就是打开数据库了.
先看下面的代码,可以成功的打开数据库.
Conn.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Main.mdb"
这句代码打开了D盘中的Main.mdb这个数据库.
Connection.Open方法的第一个参数是连接代码,它将传递给系统的数据库引擎.前半部分"Provider= Microsoft.Jet.OLEDB.4.0",它表示了数据库的类型.不同的数据库可能会不同.后半句"Source=d:\main.mdb"它表示了数据库所在的绝对路径.
打开数据库之后,还要打开表.假如数据库中有一个表,表名为"Users",字段有两个,一个为用户名,一个为密码.那么看以下代码.
1.想返回"Users"中,[用户名]为"去年烟花"的[密码]
Recordset.open "Select 密码 From Users Where 用户名='去年烟花'",Connection,1,1
之后我们就可以把用户输入的密码进行比较,看是否允许登录.
If Recordset.eof and Recordset.bof then
Msgbox "用户不存在!",16
Else
If PassWord =Recordset("密码").value then
msgbox "登录成功!",64
Else
msgbox "密码错误!",32
End If
End If
Recordset.Close
2.假设Admin已经成功登录系统,我们想把所有的用户名和密码都显示出来
Recordset.open "Select * From Users",Connection,1,1
这时,表已经被打开,我们就用以下代码把它显示出来.
Do whlie Not Recordset.eof
Print "用户名: " & Recordset("用户名").value & "密码: " & Recordset("密码").value
Recordset.MoveNext
Loop
Recordset.Close
由以上代码示例可以看出,打开表时,可以只打开其中的一个字段,也可以打开所有.第一个参数是SQL语句.
Select [字段名] From 表名 [Where 条件]
这里的条件可以省略.且字段名也可以用"*"来代替所有字段.
需要注意的是,如果你用(1)中的方法打开,那么(2)后面显示的代码就不能再用在(1)中.因为(1)里并没有打开[用户名]字段,所以这一句Recordset("密码")就没有值存在,还有可能出错.
后面的条件,可以用"="、">"、"<"等运算符.比如 "Where ID > 32".(这里假设[ID]为数字型.)
这是打开的部分.第二个很重要的部分就是查询记录.
数据库它并不是把所有记录全部放到一个变量中备用的.而是以"当前记录"的形式来返回一个值.所以我们想从中找到有用的信息,就必须要对信息进行定位/筛选.
定位:
移动到下一条 Recordset.MoveNext
移动到上一条 Recordset.MovePrevious
移动到最后一条 Recordset.MoveLast
移动到第一条 Recordset.MoveFrist
移动到某一条 Recordset.Move Number
筛选:
Recordset.Find "条件"
如:[用方法(2)打开表之后]
Private Sub Command1_Click()
Recordset.Find "用户名=" & "text1.text"
If Recordset.Eof <> True Then
Msgbox "该用户的密码是:" & Recordset("密码").value,64
Else
Msgbox "未找到该用户的资料!",16
End If
End Sub
MoveNext 只有当Eof不为True时,才可用,否则发生错误.而MovePrevious刚是Bof不为True时....
而只要Eof 和 Bof中有一个不为真时,也就是说只要有一条记录时,它就可以使用.
Find 方法中的条件和Open时的第一个参数中的条件表述方法是完全一致的.当在已打开的记录集中,找不到该记录时,Eof为True.找到则当前的值就是符合条件的记录.
第三个部分就是添加/修改记录.
修改记录很简单,先按以上的方法找到相关记录之后,给记录赋值就可以了.
比如:[(修改密码)按方法(1)打开表之后]
Recordset("密码").value = "123456"
Recordset.Updata
需要注意的就是,在修改完成后,要调用Updata方法,这样修改才能生效.
而添加记录则可以用以下代码来实现:
Recordset.addnew
Recordset("用户名").value = "Admin"
Recordset("密码").value = "Admin"
Recordset.Updata
这里,先要调用Addnew方法,增加一条新记录,然后对这个新记录中的各字段赋值,最后再调用Updata方法.
到这里就差不多了,最后说一下上面提到的几个方法.
Recordset.Open SQL语句,数据源,游标类型,打开方法
SQL语句不用说了,就是Select那啥的,目的就是按要求从表中返回数据
数据源就是一个打开之后的Connection对象.
去他妈的游标类型,填1就可以了 [偷笑ing]
打开方法对应了几个常数,具体哪几个可以从对象浏览器里看.
对应数值的意义:
1 只读 2 独占 3 可写 4 自已可写,别人可读
Connection.open 连接代码,服务器用户名,密码
这里的连接代码就不在多说了,服务器用户名,密码只有在连接远程数据库时才用到.
B. VB.NET通过调用模块怎么建立access数据库
oledb的使用网上的确很多.
如果只是查询. 写入. 删除,我们常用的就是
OleDbConnection '用于写数据库的连接
OleDbCommand '用于数据的操作 接收SQL语句 并执行
OleDbDataReader '查询了,我们就用这个类来读取查询的对象
1).OleDbConnection
dim conn as new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0;Data Source =数据库的路径")'连接字符串一般就这样
查询前在打开
conn.open
2).OleDbCommand
dim cmd as new OleDbCommand ("SQL语句",conn)'SQL语句决定了你是查询写入还是更新删除
'''
如果是查询,你还要用到OleDbDataReader
这个是不用新建的不能用New
dim rd as OleDbDataReader=cmd.ExecuteReader'cmd.ExecuteReader 方法就是读取查询的内容
cmd.ExecuteNonQuery用于执行操作 返回变动的条数 ,一般用于 添加 删除 更新 等其它操作
其实就这些了,就是要学点SQL语句就行了.
标准执行过程就是
Dim conn As New OleDbConnection("连接字符串")
conn.Open()
Dim cmd As New OleDbCommand("SQL语句", conn)
'如果是写入 更新 删除 则
cmd.ExecuteNonQuery()
' 否则
Dim rd As OleDbDataReader = cmd.ExecuteReader
If rd.Read Then
Dim d As Object = rd.Item("字段名")
End If
' 结束如果
rd.Close()
cmd.Dispose()
conn.Close()
上面要有 Imports System.Data.OleDb
C. VB.NET如何生成ACCESS数据库并生成表。 VB.NET
我的需求就是先填写好表和表中字段的名称,然后点击一个按钮,就在指定的路径生成一个ACCESS数据库。而不是事先手动创建。 请知道的各位附上完整代码,本人刚接触VB.NET,有些东西还不是很了解。谢谢。
D. vb.net 建立数据库(access),并添加一条数据进数据库的代码'20分
....你参考参考吧:
数据库结构:
--------------------------------------
test.mdb [放在BIN目录下]
表 test(id 自动编号, img OLE)
----------------------------------------------
代码:
----------------------------------------------------
使用 一个 openfiledialog ,两个 picturebox
----------------------------------------------------------------------
Imports System.IO
Public Class Form1
Inherits System.Windows.Forms.Form
Dim cnn As Data.OleDb.OleDbConnection
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Me.Dispose(True)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If OpenFileDialog1.ShowDialog = DialogResult.OK Then
PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
End If
End Sub
Private Sub DBInit()
Try
cnn = New Data.OleDb.OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;Data Source=" & Application.StartupPath & "\test.mdb")
cnn.Open()
Catch exp As OleDb.OleDbException
MsgBox(exp.Message)
End
End Try
End Sub
Private Sub DBRelease()
cnn.Close()
cnn = Nothing
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If PictureBox1.Image Is Nothing Then
MsgBox("请先选择图片", MsgBoxStyle.Exclamation)
Exit Sub
End If
Dim fs As FileStream = New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read)
Dim bt(fs.Length) As Byte
fs.Read(bt, 0, fs.Length)
fs.Close()
fs = Nothing
Dim oleCmd As OleDb.OleDbCommand = New OleDb.OleDbCommand
DBInit()
oleCmd.Connection = cnn
oleCmd.CommandType = CommandType.Text
oleCmd.CommandText = "INSERT INTO test (img) VALUES (@img)"
oleCmd.Parameters.Add("@img", OleDb.OleDbType.Binary).Value = bt
oleCmd.ExecuteNonQuery()
oleCmd = Nothing
DBRelease()
MsgBox("图片插入成功")
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim oleCmd As OleDb.OleDbCommand = New OleDb.OleDbCommand("SELECT img FROM test WHERE id=1")
oleCmd.CommandType = CommandType.Text
DBInit()
oleCmd.Connection = cnn
Dim dr As OleDb.OleDbDataReader = oleCmd.ExecuteReader(CommandBehavior.SingleRow)
If dr.Read Then
If Not IsDBNull(dr.Item(0)) Then
Dim bt() As Byte = dr.Item(0)
Dim ms As MemoryStream = New MemoryStream(bt)
PictureBox2.Image = Image.FromStream(ms)
Else
MsgBox("无图片")
End If
Else
MsgBox("无数据")
End If
dr.Close()
dr = Nothing
oleCmd = Nothing
DBRelease()
End Sub
End Class
E. vb.net 写数据库
你这是向sql后台数据库写数据吗?
sqldataadapter1.update 改须指定有效的updatecommand,我一直没有看到
给你个例子你看吧:
'首先指定有效的UpdateCommand
sqlstr = "insert into Playlist(PlayName,PlayPath)values('" & TextBox4.Text.Trim & "','" & TextBox5.Text.Trim & "')"
Mole1.Mycommand.CommandText = sqlstr
'增加新一行(此行必须,要不不能与后台同步,并且后面调用update会出错)
Mole1.MyDataSet.Tables("Music").Rows.Add()
Mole1.MyDataAdaPter.InsertCommand = Mole1.Mycommand
Mole1.MyDataAdaPter.Update(Mole1.MyDataSet, "Music")
MsgBox("增加数据成功!!")
F. vb.net数据库操作
参考一下下面这段代码就可以了。
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
G. vb.net 创建ACCESS数据库。大神给点代码,学习学习。
这也是我研究很长时间的成果,差不多分得给我吧。\(^o^)/~
Imports System.Data
Public Class Form1
Public dt As New DataTable
Public dr As DataRow
'内存表格及界面表格初始化,分类列表框初始化
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.SetDesktopBounds(283, 84, 800, 600)
With dt
.Clear()
.Columns.Add("ID")
.Columns.Add("故障描述")
.Columns.Add("原因分析")
.Columns.Add("排除办法")
.Columns.Add("备注")
.Columns.Add("时间")
End With
With Me.DataGridView1
.DataSource = dt
.Columns(0).Width = 30
.Columns(1).Width = 120
.Columns(2).Width = 150
.Columns(3).Width = 190
.Columns(4).Width = 65
.Columns(5).Width = 150
End With
With TS_ComboBox1
.Items.Clear()
.Items.Add("电气")
.Items.Add("机械")
.Items.Add("操作")
End With
End Sub
'将表格中的数据显示到文本框中
Private Sub DataGridView1_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.) Handles DataGridView1.CellMouseClick
If Not IsDBNull(DataGridView1.CurrentRow.Cells(1).Value) Then
RichTextBox1.Text = DataGridView1.CurrentRow.Cells(1).Value
End If
If Not IsDBNull(DataGridView1.CurrentRow.Cells(2).Value) Then
RichTextBox2.Text = DataGridView1.CurrentRow.Cells(2).Value
End If
If Not IsDBNull(DataGridView1.CurrentRow.Cells(3).Value) Then
RichTextBox3.Text = DataGridView1.CurrentRow.Cells(3).Value
End If
If Not IsDBNull(DataGridView1.CurrentRow.Cells(4).Value) Then
RichTextBox4.Text = DataGridView1.CurrentRow.Cells(4).Value
End If
End Sub
Private Sub MenuItem5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem5.Click
Dim str
str = MsgBox("您确定要退出系统吗?", vbOKCancel + vbQuestion, "系统提示")
If str = vbOK Then
End
End If
End Sub
'查看所有记录
Private Sub MenuItem2_1_1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2_1_1.Click
Dim strsql
Dim rs As OleDb.OleDbDataReader
strsql = "select * from Fault_Analyse order by ID"
transactsql(strsql)
rs = oledbcmd.ExecuteReader
dt.Clear()
Do Until Not rs.Read() '在这里作为条件的同时,也执行了该语句
dr = dt.NewRow
dr.Item(0) = rs.Item(0)
dr.Item(1) = rs.Item(2)
dr.Item(2) = rs.Item(3)
dr.Item(3) = rs.Item(4)
dr.Item(4) = rs.Item(5)
dr.Item(5) = rs.Item(1)
dt.Rows.Add(dr)
Loop
DataGridView1.Refresh()
oledbcmd.Dispose() '释放后不能读值
dbconnection.Close()
dbconnection.Dispose()
End Sub
'工具栏隐藏与显示操作
Private Sub MenuItem3_1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3_1.Click
If Me.MenuItem3_1.Checked = True Then
Me.MenuItem3_1.Checked = False
Else
Me.MenuItem3_1.Checked = True
End If
If Me.MenuItem3_1.Checked = True Then
ToolStrip1.Visible = True
Else
ToolStrip1.Visible = False
End If
End Sub
'往Access数据库中插入新行
Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
Dim strsql As Object
Dim rs As OleDb.OleDbDataReader
Dim datetime As Date
Dim describe_fault, analyse_reason, solution, remarks As String
Dim class_g1, class_g2 As String
Dim i, j As Integer
i = 0 '初始化
describe_fault = RichTextBox1.Text
analyse_reason = RichTextBox2.Text
solution = RichTextBox3.Text
remarks = RichTextBox4.Text
class_g1 = TS_ComboBox1.Text
class_g2 = TS_ComboBox2.Text
datetime = Now
'注意格式
strsql = "select ID from Fault_Analyse" 'where ID =" + ID + ""
transactsql(strsql)
rs = oledbcmd.ExecuteReader
Do Until Not rs.Read() '获取记录的行数
i = i + 1
Loop
If i = 0 Then i = 1
For j = 1 To i '查询表中是否有ID为j的记录,没有记录则定义新行j
strsql = "select ID from Fault_Analyse where ID =" + CStr(j) + ""
transactsql(strsql)
rs = oledbcmd.ExecuteReader
If Not rs.Read() Then
Num1.Value = CInt(j)
Exit For
End If
Next
If TS_ComboBox1.Text <> "" And TS_ComboBox2.Text <> "" Then
strsql = "Insert into Fault_Analyse values('" + CStr(j) + "','" + datetime + " ','" _
+ describe_fault + " ','" + analyse_reason + " ','" + solution + " ','" _
+ remarks + "','" + class_g1 + "','" + class_g2 + "','0')"
transactsql(strsql)
oledbcmd.Dispose() '释放后不能读值
dbconnection.Close()
dbconnection.Dispose()
Else
MsgBox("请为输入的内容分类。", vbOK + vbInformation, "系统提示")
End If
End Sub
'从数据库中删除选中行
Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
Dim strsql
Dim str
str = MsgBox("确定要删除- " + CStr(RichTextBox1.Text) + " -的内容吗?", vbOKCancel + vbQuestion, "系统提示")
If str = vbOK Then
strsql = "delete from Fault_Analyse where describe_fault='" + CStr(RichTextBox1.Text) + "'"
transactsql(strsql)
oledbcmd.Dispose() '释放后不能读值
dbconnection.Close()
dbconnection.Dispose()
End If
End Sub
'按照分类查询数据库中指定内容
Private Sub ToolStripButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton4.Click
Dim strsql
Dim rs As OleDb.OleDbDataReader
If TS_ComboBox1.Text = "" Or TS_ComboBox2.Text = "" Then
MsgBox("请选择您要查询的类别", vbOKCancel + vbInformation, "系统提示")
Else
strsql = "select * from Fault_Analyse where class_g1='" _
+ TS_ComboBox1.Text + "'and class_g2='" + TS_ComboBox2.Text + "' order by ID"
transactsql(strsql)
rs = oledbcmd.ExecuteReader
dt.Clear()
Do Until Not rs.Read() '在这里作为条件的同时,也执行了该语句
dr = dt.NewRow
dr.Item(0) = rs.Item(0)
dr.Item(1) = rs.Item(2)
dr.Item(2) = rs.Item(3)
dr.Item(3) = rs.Item(4)
dr.Item(4) = rs.Item(5)
dr.Item(5) = rs.Item(1)
dt.Rows.Add(dr)
Loop
DataGridView1.Refresh()
oledbcmd.Dispose() '释放后不能读值
dbconnection.Close()
dbconnection.Dispose()
End If
End Sub
'界面文本框内容清空
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim str
str = MsgBox("确定要清空文本框中内容吗?", vbOKCancel + vbQuestion, "系统提示")
If str = vbOK Then
RichTextBox1.Text = " "
RichTextBox2.Text = " "
RichTextBox3.Text = " "
RichTextBox4.Text = " "
End If
End Sub
'分类选择列表框的定义
Private Sub TS_ComboBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TS_ComboBox1.TextChanged
TS_ComboBox2.Text = ""
If TS_ComboBox1.Text = "电气" Then
With TS_ComboBox2.Items
.Clear()
.Add("程序")
.Add("设备")
.Add("方案")
.Add("工艺")
.Add("其它")
End With
ElseIf TS_ComboBox1.Text = "机械" Then
With TS_ComboBox2.Items
.Clear()
.Add("设计")
.Add("质量")
.Add("故障")
.Add("其它")
End With
ElseIf TS_ComboBox2.Text = "操作" Then
With TS_ComboBox2.Items
.Add("熟练度")
.Add("方案")
.Add("其它")
End With
End If
End Sub
'工具栏的显示与隐藏
Private Sub ToolStripButton5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton5.Click
MenuItem3_1.Checked = False
ToolStrip1.Visible = False
End Sub
End Class
H. vb.net怎么动态创建access数据库和表
追问: 只有那个办法? 回答: 是,这跟SQL Server不同,ACCESS是独立文件的,而且它能支持的SQL语句有限,一般是使用ACCESS来创建,若要动态创建,只能用ADO了,但Sql Server就不同了,它可以执行复杂的SQL语句,相对来说,动态创建数据库和表,要比ACCESS方便。 追问: 创建表的话能用SQL语句了吗? 回答: 没试过,应该不行 追问: 那你能告诉我下怎么创建表吗?创建数据库我会了 回答: 可以使用DAO对象来操作,注意是DAO对象,不是ADO对象! 追问: 能不能详细说下过程呢?创建表的过程 回答: 创建表的过程用DAO.net和ADO.net都可以,创建数据库可以用DAO对象,既然你已经可以创建数据库,那么创建表只要先连接到这个数据库,然后用create table语句来执行就行了,可以使用OledbConnection先连接这个库,接着用OledbCommand对象的ExecuteNonQuery来执行create table语句即可。