数据库修改问题“大神看看,为何修改不了数据库的数据”
Public Class 修改单位信息Private usernameid As String
Private sqlstring As String
Private Property Loginname As String
Private connstring As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\编程设计\单位信息管理.accdb"
Private Sub 修改单位信息_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ComboBox1.Enabled = False
Dim sqlstring As String = "select 单位性质 from 单位性质 "
Dim conn As New OleDb.OleDbConnection(connstring)
Dim cmd As New OleDb.OleDbCommand(sqlstring, conn)
conn.Open()
Dim num As Integer = 0
Dim reader As OleDb.OleDbDataReader
reader = cmd.ExecuteReader
ComboBox2.Items.Clear()
While (reader.Read())
ComboBox2.Items.Add(reader.GetValue(0))
num = num + 1
End While
reader.Close()
conn.Close()
ComboBox2.Text = ""
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim conn As New OleDb.OleDbConnection(connstring)
Dim sqlstring As String
Dim reader As OleDb.OleDbDataReader
'添加数据到数据库()
sqlstring = "select 单位名称 from 单位信息 where 单位名称='" & ComboBox1.Text & "'"
'执行查询
Dim rscomd As New OleDb.OleDbCommand(sqlstring, conn)
conn.Open() '连接数据库
reader = rscomd.ExecuteReader '执行查询返回查询结果
If reader.Read() Then '判断查询结果
sqlstring = "update 单位信息 set 单位信息.单位名称='" & TextBox1.Text & "',单位信息.单位负责人='" & TextBox2.Text & "',单位信息.单位性质='" & TextBox3.Text & "',单位信息.单位座机='" & TextBox4.Text & "',单位信息.联系人='" & TextBox5.Text & "',单位信息.联系手机='" & TextBox6.Text & "',单位信息.备注='" & TextBox7.Text & "' where 单位名称 = ' " & ComboBox1.Text & "'"
Try
Dim cmd As New OleDb.OleDbCommand(sqlstring, conn)
cmd.ExecuteNonQuery()
reader.Close()
conn.Close()
MsgBox("添加数据成功!", vbOKOnly, "提示")
Catch ex As Exception
MsgBox(ex.Message)
End Try
Me.Close()
End If
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
TextBox6.Text = ""
TextBox7.Text = ""
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim a As String = ComboBox1.Text
Dim b As String = ComboBox2.Text
Dim sqlstring1 As String = "select * from 单位信息 where 单位名称='" & a & "'"
Dim conn1 As New OleDb.OleDbConnection(connstring)
Dim cmd1 As New OleDb.OleDbCommand(sqlstring1, conn1)
conn1.Open()
Dim num1 As Integer = 0
Dim reader1 As OleDb.OleDbDataReader
reader1 = cmd1.ExecuteReader
ComboBox1.Items.Clear()
While (reader1.Read())
TextBox1.Text = reader1.GetValue(1)
TextBox2.Text = reader1.GetValue(2)
TextBox3.Text = reader1.GetValue(3)
TextBox4.Text = reader1.GetValue(5)
TextBox5.Text = reader1.GetValue(4)
TextBox6.Text = reader1.GetValue(6)
TextBox7.Text = reader1.GetValue(7)
num1 = num1 + 1
End While
reader1.Close()
conn1.Close()
' liyunfeng = ComboBox1.Text
ComboBox1.Text = ""
ComboBox2.Text = ""
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
If ComboBox2.Text <> "" Then
ComboBox1.Enabled = True
Dim a As String = ComboBox2.Text
Dim sqlstring1 As String = "select 单位名称 from 单位信息 where 单位性质='" & a & "'"
Dim conn1 As New OleDb.OleDbConnection(connstring)
Dim cmd1 As New OleDb.OleDbCommand(sqlstring1, conn1)
conn1.Open()
Dim num1 As Integer = 0
Dim reader1 As OleDb.OleDbDataReader
reader1 = cmd1.ExecuteReader
ComboBox1.Items.Clear()
While (reader1.Read())
ComboBox1.Items.Add(reader1.GetValue(0))
num1 = num1 + 1
End While
reader1.Close()
conn1.Close()
Else
MsgBox("请选择单位性质!")
End If
ComboBox1.Text = ""
End Sub
End Class