Imports System.ComponentModel
Imports System.Data.OleDb ' 使用 OleDb 代替 SqlClient(适用于 Access)
Public Class 数据录入
' 添加一个标志变量,记录是否处于可编辑状态
Private isEditing As Boolean = False
' 数据库连接字符串(Access 数据库路径,请替换成你的实际路径)
Private connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\18202\Documents\Database2.mdb;"
' 如果使用 Access 2007 或更高版本(.accdb 格式),改用:
' Private connectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\你的路径\Database2.accdb;"
Private Sub 数据录入_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' 初始状态下禁用所有输入控件
DisableInputControls()
End Sub
' 禁用所有输入控件的方法
Private Sub DisableInputControls()
' 示例(请替换为你的实际控件):
TextBox1.Enabled = False
TextBox2.Enabled = False
TextBox3.Enabled = False
TextBox4.Enabled = False
TextBox5.Enabled = False
TextBox6.Enabled = False
TextBox7.Enabled = False
TextBox8.Enabled = False
TextBox9.Enabled = False
TextBox10.Enabled = False
TextBox11.Enabled = False
TextBox12.Enabled = False
TextBox13.Enabled = False
TextBox14.Enabled = False
ComboBox1.Enabled = False
ComboBox2.Enabled = False
ComboBox3.Enabled = False
ComboBox4.Enabled = False
DateTimePicker1.Enabled = False
DateTimePicker2.Enabled = False
DateTimePicker3.Enabled = False
DateTimePicker4.Enabled = False
DateTimePicker5.Enabled = False
DateTimePicker6.Enabled = False
End Sub
' 启用所有输入控件的方法
Private Sub EnableInputControls()
' 示例(请替换为你的实际控件):
TextBox1.Enabled = True
TextBox2.Enabled = True
TextBox3.Enabled = True
TextBox4.Enabled = True
TextBox5.Enabled = True
TextBox6.Enabled = True
TextBox7.Enabled = True
TextBox8.Enabled = True
TextBox9.Enabled = True
TextBox10.Enabled = True
TextBox11.Enabled = True
TextBox12.Enabled = True
TextBox13.Enabled = True
TextBox14.Enabled = True
ComboBox1.Enabled = True
ComboBox2.Enabled = True
ComboBox3.Enabled = True
ComboBox4.Enabled = True
DateTimePicker1.Enabled = True
DateTimePicker2.Enabled = True
DateTimePicker3.Enabled = True
DateTimePicker4.Enabled = True
DateTimePicker5.Enabled = True
DateTimePicker6.Enabled = True
End Sub
' 清空所有输入控件的方法
' 清空所有输入控件的方法
Private Sub ClearInputControls()
' 清空所有 TextBox
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
TextBox6.Text = ""
TextBox7.Text = ""
TextBox8.Text = ""
TextBox9.Text = ""
TextBox10.Text = ""
TextBox11.Text = ""
TextBox12.Text = ""
TextBox13.Text = ""
TextBox14.Text = ""
' 重置 ComboBox 选择
ComboBox1.SelectedIndex = -1
ComboBox2.SelectedIndex = -1
ComboBox3.SelectedIndex = -1
ComboBox4.SelectedIndex = -1
' 重置 DateTimePicker 为当前日期
DateTimePicker1.Value = DateTime.Now
DateTimePicker2.Value = DateTime.Now
DateTimePicker3.Value = DateTime.Now
DateTimePicker4.Value = DateTime.Now
DateTimePicker5.Value = DateTime.Now
DateTimePicker6.Value = DateTime.Now
End Sub
' 添加按钮点击事件
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles Button3.Click
isEditing = True
EnableInputControls()
ClearInputControls() ' 清空旧数据
MessageBox.Show("现在可以开始录入数据", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
' 保存按钮点击事件(修改为 Access 数据库存储逻辑)
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Not isEditing Then
MessageBox.Show("请先点击添加按钮开始录入", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Return
End If
' 数据验证(示例)
' If String.IsNullOrEmpty(TextBox1.Text) Then
' MessageBox.Show("请输入姓名", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)
' Return
' End If
' Access 数据库存储逻辑
Try
Using connection As New OleDbConnection(connectionString)
connection.Open()
' 使用更安全的参数化查询
Dim sql As String = "INSERT INTO [表1] (" &
"[课题名称], [申请立项时间], [立项批准时间], [学科分类], [课题编号], " &
"[课题批准单位], [课题类别], [主持人], [职称], [职务], " &
"[预计完成时间], [参与者], [课题成果], [课题进款总额], [进款时间], " &
"[进款来源], [进款单号], [项目花销明细], [经费结余], " &
"[结题申报时间], [结题审批时间], [结题证号], [备注]) " &
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
Using command As New OleDbCommand(sql, connection)
' 按顺序添加参数(必须与SQL中的字段顺序完全一致)
' 文本型字段
command.Parameters.AddWithValue("@p1", TextBox1.Text) ' 课题名称
command.Parameters.AddWithValue("@p2", If(DateTimePicker1.Checked, DateTimePicker1.Value, DBNull.Value)) ' 申请立项时间
command.Parameters.AddWithValue("@p3", If(DateTimePicker2.Checked, DateTimePicker2.Value, DBNull.Value)) ' 立项批准时间
command.Parameters.AddWithValue("@p4", ComboBox1.SelectedItem?.ToString()) ' 学科分类
command.Parameters.AddWithValue("@p5", TextBox2.Text) ' 课题编号
command.Parameters.AddWithValue("@p6", TextBox3.Text) ' 课题批准单位
command.Parameters.AddWithValue("@p7", ComboBox2.SelectedItem?.ToString()) ' 课题类别
command.Parameters.AddWithValue("@p8", TextBox4.Text) ' 主持人
command.Parameters.AddWithValue("@p9", TextBox5.Text) ' 职称
command.Parameters.AddWithValue("@p10", TextBox6.Text) ' 职务
command.Parameters.AddWithValue("@p11", If(DateTimePicker3.Checked, DateTimePicker3.Value, DBNull.Value)) ' 预计完成时间
command.Parameters.AddWithValue("@p12", TextBox7.Text) ' 参与者
command.Parameters.AddWithValue("@p13", TextBox8.Text) ' 课题成果
' 数值型字段
command.Parameters.Add("@p14", OleDbType.Decimal).Value = If(Decimal.TryParse(TextBox9.Text, Nothing), TextBox9.Text, DBNull.Value) ' 课题进款总额
' 日期型字段
command.Parameters.AddWithValue("@p15", If(DateTimePicker4.Checked, DateTimePicker4.Value, DBNull.Value)) ' 进款时间
' 文本型字段
command.Parameters.AddWithValue("@p16", TextBox10.Text) ' 进款来源
command.Parameters.AddWithValue("@p17", TextBox11.Text) ' 进款单号
command.Parameters.AddWithValue("@p18", TextBox12.Text) ' 项目花销明细
' 数值型字段
command.Parameters.Add("@p19", OleDbType.Decimal).Value = If(Decimal.TryParse(TextBox13.Text, Nothing), TextBox13.Text, DBNull.Value) ' 经费结余
' 日期型字段
command.Parameters.AddWithValue("@p20", If(DateTimePicker5.Checked, DateTimePicker5.Value, DBNull.Value)) ' 结题申报时间
command.Parameters.AddWithValue("@p21", If(DateTimePicker6.Checked, DateTimePicker6.Value, DBNull.Value)) ' 结题审批时间
' 文本型字段
command.Parameters.AddWithValue("@p22", TextBox14.Text) ' 结题证号
command.Parameters.AddWithValue("@p23", ComboBox3.SelectedItem?.ToString() ) ' 备注
Dim rowsAffected As Integer = command.ExecuteNonQuery()
If rowsAffected > 0 Then
MessageBox.Show("数据保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
isEditing = False
DisableInputControls()
Else
MessageBox.Show("数据保存失败", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Using
End Using
Catch ex As Exception
MessageBox.Show("数据库错误: " & ex.Message & vbCrLf & "堆栈跟踪: " & ex.StackTrace,
"错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
' 取消按钮点击事件
Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles Button2.Click
If isEditing Then
Dim result = MessageBox.Show("确定要取消当前输入的内容吗?", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If result = DialogResult.Yes Then
ClearInputControls() ' 清空所有输入
isEditing = False
DisableInputControls() ' 恢复不可编辑状态
End If
Else
MessageBox.Show("当前没有正在编辑的数据", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
' 关闭窗口事件
Private Sub 数据录入_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
If isEditing Then
Dim result = MessageBox.Show("当前有未保存的数据,确定要关闭窗口吗?", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation)
If result = DialogResult.Cancel Then
e.Cancel = True
End If
Else
Dim result = MessageBox.Show("是否要关闭窗口?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation)
If result = DialogResult.Cancel Then
e.Cancel = True
End If
End If
End Sub
End Class数据库有问题