重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
我的需求就是先填写好表和表中字段的名称,然后点击一个按钮,就在指定的路径生成一个ACCESS数据库。而不是事先手动创建。 请知道的各位附上完整代码,本人刚接触VB.NET,有些东西还不是很了解。谢谢。
我们提供的服务有:网站设计、成都做网站、微信公众号开发、网站优化、网站认证、泰和ssl等。为1000+企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的泰和网站制作公司
oledb的使用网上的确很多.\x0d\x0a如果只是查询. 写入. 删除,我们常用的就是\x0d\x0aOleDbConnection '用于写数据库的连接\x0d\x0aOleDbCommand '用于数据的操作 接收SQL语句 并执行\x0d\x0aOleDbDataReader '查询了,我们就用这个类来读取查询的对象\x0d\x0a1).OleDbConnection \x0d\x0a dim conn as new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0;Data Source =数据库的路径")'连接字符串一般就这样 \x0d\x0a 查询前在打开 \x0d\x0a conn.open\x0d\x0a 2).OleDbCommand \x0d\x0a dim cmd as new OleDbCommand ("SQL语句",conn)'SQL语句决定了你是查询写入还是更新删除\x0d\x0a '''\x0d\x0a如果是查询,你还要用到OleDbDataReader \x0d\x0a 这个是不用新建的不能用New \x0d\x0adim rd as OleDbDataReader=cmd.ExecuteReader'cmd.ExecuteReader 方法就是读取查询的内容\x0d\x0acmd.ExecuteNonQuery用于执行操作 返回变动的条数 ,一般用于 添加 删除 更新 等其它操作\x0d\x0a \x0d\x0a其实就这些了,就是要学点SQL语句就行了.\x0d\x0a标准执行过程就是\x0d\x0a Dim conn As New OleDbConnection("连接字符串")\x0d\x0a conn.Open()\x0d\x0a Dim cmd As New OleDbCommand("SQL语句", conn)\x0d\x0a '如果是写入 更新 删除 则\x0d\x0a cmd.ExecuteNonQuery()\x0d\x0a 否则\x0d\x0a Dim rd As OleDbDataReader = cmd.ExecuteReader\x0d\x0a \x0d\x0a If rd.Read Then\x0d\x0a Dim d As Object = rd.Item("字段名")\x0d\x0a End If\x0d\x0a 结束如果\x0d\x0a rd.Close()\x0d\x0a cmd.Dispose()\x0d\x0a conn.Close()\x0d\x0a \x0d\x0a上面要有 Imports System.Data.OleDb
这也是我研究很长时间的成果,差不多分得给我吧。\(^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.DataGridViewCellMouseEventArgs) 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
追问: 只有那个办法? 回答: 是,这跟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语句即可。