标题:数据库修改问题“大神看看,为何修改不了数据库的数据”
只看楼主
pcliyunfeng
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2013-5-2
结帖率:0
已结贴  问题点数:20 回复次数:9 
数据库修改问题“大神看看,为何修改不了数据库的数据”
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
搜索更多相关主题的帖子: 单位 信息 False 
2013-05-02 10:22
不说也罢
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:39
帖 子:1481
专家分:4989
注 册:2007-10-7
得分:20 
代码太长,没看完,修改数据的代码应当是在Button1_Click过程里,把TRY注释掉,有返回错误信息吗?

===================================================
讨厌C#的行尾的小尾巴;和一对大括号{ }
===================================================
2013-05-02 12:03
pcliyunfeng
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2013-5-2
得分:0 
回复 2楼 不说也罢
运行的时候很正常,也显示修改数据完成提示框,就是数据库里面的数据并没有修改,还是修改前的数据
2013-05-02 13:39
pcliyunfeng
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2013-5-2
得分:0 
以下是引用不说也罢在2013-5-2 12:03:28的发言:

代码太长,没看完,修改数据的代码应当是在Button1_Click过程里,把TRY注释掉,有返回错误信息吗?



注销后,什么也不显示,好像就没有执行if循环
2013-05-02 13:42
不说也罢
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:39
帖 子:1481
专家分:4989
注 册:2007-10-7
得分:0 
你可以在IF语句之前msgbox(reader.HasRows),看看返回值是false还是true
可能是IF条件没有被满足

===================================================
讨厌C#的行尾的小尾巴;和一对大括号{ }
===================================================
2013-05-02 20:03
pcliyunfeng
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2013-5-2
得分:0 
以下是引用不说也罢在2013-5-2 20:03:46的发言:

你可以在IF语句之前msgbox(reader.HasRows),看看返回值是false还是true
可能是IF条件没有被满足

返回的是flash,我看了,是combobox1.text在查询的时候就清空了,我定了个通用字符串,现将combobox1.text的值赋给这个通用字符串后再清空,返回的是true,程序运行很好,也提示“数据修改成功”,就是查看数据的时候,发现并没有修改数据,还显示的是修改前的数据,看数据库的数据,也是没有修改。
2013-05-03 08:38
pcliyunfeng
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2013-5-2
得分:0 
以下是引用pcliyunfeng在2013-5-3 08:38:30的发言:


返回的是flash,我看了,是combobox1.text在查询的时候就清空了,我定了个通用字符串,现将combobox1.text的值赋给这个通用字符串后再清空,返回的是true,程序运行很好,也提示“数据修改成功”,就是查看数据的时候,发现并没有修改数据,还显示的是修改前的数据,看数据库的数据,也是没有修改。

 Dim liyunfeng As String

Dim conn As New OleDb.OleDbConnection(connstring)
        Dim sqlstring As String
        Dim reader As OleDb.OleDbDataReader
        '添加数据到数据库()
        sqlstring = "select 单位名称  from 单位信息 where 单位名称='" & liyunfeng & "'"
        '执行查询
        Dim rscomd As New OleDb.OleDbCommand(sqlstring, conn)
        conn.Open()                             '连接数据库
        reader = rscomd.ExecuteReader '执行查询返回查询结果
        MsgBox(reader.HasRows)
        If reader.Read() Then                   '判断查询结果
            sqlstring = "update 单位信息 set 单位名称='" & TextBox1.Text & "',单位负责人='" & TextBox2.Text & "',单位性质='" & TextBox3.Text & "',单位座机='" & TextBox4.Text & "',联系人='" & TextBox5.Text & "',联系手机='" & TextBox6.Text & "',备注='" & TextBox7.Text & "' where 单位名称 = ' " & liyunfeng & "'"
            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 = ""
2013-05-03 08:39
不说也罢
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:39
帖 子:1481
专家分:4989
注 册:2007-10-7
得分:0 
where 单位名称 = ' " & liyunfeng & "'"

这里似乎多了一个空格,可能会导致where条件没有满足,改成下面试试
 where 单位名称 = '" & liyunfeng & "'"

===================================================
讨厌C#的行尾的小尾巴;和一对大括号{ }
===================================================
2013-05-03 12:30
mapy06
Rank: 2
等 级:论坛游民
威 望:1
帖 子:19
专家分:43
注 册:2012-3-6
得分:0 
最简单的办法,DEBUG下跟踪SQL语句,然后COPY出来到数据库里直接运行,调试。
事半功倍。
2013-05-10 08:50
mapy06
Rank: 2
等 级:论坛游民
威 望:1
帖 子:19
专家分:43
注 册:2012-3-6
得分:0 
你复制出来这么多代码,没人有心思看。把关键的部分列出来。
2013-05-10 08:51



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-406055-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.069584 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved