vb.net 创建ACCESS数据库。大神给点代码,学习学习。

2024-12-15 08:02:41
推荐回答(2个)
回答1:

这也是我研究很长时间的成果,差不多分得给我吧。\(^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

回答2:

使用向导建立数据库连接的优点是省去了编写代码的过程中,弊端是不容易更改连接
使用代码来连接的好处,需要编写代码的,但容易改变和维持

如:位置对数据库的,显然更容易改变代码直接

代码:

程序Imports System.Data.OleDb / *参考命名空间* / 暗淡cnstr =“提供= microsoft.jet.oledb。4.0,数据源访问数据库文件位置”/ *定义连接字符串* /

昏暗的CN的OleDbConnection =新的OleDbConnection(cnstr的) / *定义的连接* /

cn.open()/ *打开的连接* /

sqlstr =“选择...从...或其他SQL”

昏暗的SqlCommand =新的SqlCommand()

CMD CMD。的CommandText = sqlstr

cmd.connection = CN

/ *执行上面的语句是建立* / p> cmd.executenonquery页()不返回结果

CMD。

cmd.executereader()返回的结果集

/ *这些方法执行SQL语句* /

cn.close()/的ExecuteScalar()返回单个结果*关闭连接* /

用户身份验证,那么你所要做的,首先检查用户名对应的密码,然后确定用户是否输入密码,查询结果是不一样的,可以