注册 登录
编程论坛 VB.NET论坛

[VB2010+Access2007编程]数据添加成功,但数据库记录无变化,原因在哪里?

SCQWT 发布于 2016-11-21 19:42, 3998 次点击
请各位大侠帮我看看一下代码,数据添加提示成功,但数据库记录无变化,原因在哪里?
Private Sub btnOk1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk1.Click
        Cnn.ConnectionString =
         = "Select 用户名,密码,职务  From tb_user WHERE 用户名='" & txtUsername.Text & "'"
        Cmd.Connection = Cnn
        DBAdapter.SelectCommand = Cmd
        Dim builder As OleDb.OleDbCommandBuilder = New OleDb.OleDbCommandBuilder(DBAdapter)
        Cnn.Open()
        If txtUsername.Text = "" Or txtPwd.Text = "" Or postComboBox.Text = "" Then
            Cnn.Close()
            MsgBox("请填写完整信息!", MsgBoxStyle.Exclamation, "消息框")
        ElseIf Cmd.ExecuteReader.Read Then
            Cnn.Close()
            MsgBox("用户 [ " & txtUsername.Text & " ] 已存在,请重新输入。", vbOKOnly, "系统提示")
        Else
            Cnn.Close()
            Dim response As MsgBoxResult
            response = MsgBox("确定要添加该用户吗?", vbOKCancel + vbQuestion, "系统提示")
            If response = MsgBoxResult.Ok Then                      ' 用户选择“确定”
                '添加用户信息
                AddUser()
                txtUsername.Clear()
                txtPwd.Clear()
                postComboBox.Text = ""
            Else
                Exit Sub
            End If
        End If
    End Sub

    Sub AddUser()
        Dim SQLString As String
        SQLString = "INSERT INTO tb_user VALUES('" & txtUsername.Text & "','" & txtPwd.Text & "','" & postComboBox.Text & "')"
        Cnn.Open()
        Cmd = New OleDbCommand(SQLString, Cnn)
        Cmd.ExecuteNonQuery()
        MessageBox.Show("添加成功!")
        Cnn.Close()
        Cnn.Dispose()
    End Sub
5 回复
#2
不说也罢2016-11-22 08:53
Cmd.ExecuteNonQuery()    '这一行代码被成功执行(没有错误提示),不代表数据库操作成功。它的返回值表示数据库中受影响的行数。
'如果你直接打开数据库中的表,发现没有插入新的行,这行代码的返回值就会是0
 MessageBox.Show("添加成功!")

这两行代码你用下面的替换下,看看运行结果:
Dim a As Integer=Cmd.ExecuteNonQuery()
MessageBox.Show(a.ToString())'估计是0

猜测其原因,是你的INSERT INTO语句写的不规范,或者数据类型有冲突
 INSERT INTO 表名(field1,field2,...) values(value1,value2,...),注意字段的对应及顺序关系
#3
vailer2016-11-25 15:35
你可以dim 一个返回值ingter,然后返回值=Cmd.ExecuteNonQuery(),就可以知道是不是真的成功了,如果返回值是0,那就是没成功,最容易错的地方:看下你的'是英文的还是中文的,'和’不一样的喔


[此贴子已经被作者于2016-11-25 15:37编辑过]

#4
SCQWT2016-12-05 18:51
回复 2楼 不说也罢
你好,我用你的两行代码替换了我的两行代码,对话框返回值是1,请问是什么情况呢?
#5
SCQWT2016-12-05 19:00
回复 2楼 不说也罢
谢谢各位高手,欢迎继续指导,谢谢。

[此贴子已经被作者于2016-12-5 19:01编辑过]

#6
com343142017-05-23 21:03
Cnn.ConnectionString =
          = "Select 用户名,密码,职务  From tb_user WHERE 用户名='" & txtUsername.Text & "'"
         Cmd.Connection = Cnn
         DBAdapter.SelectCommand = Cmd
         Dim builder As OleDb.OleDbCommandBuilder = New OleDb.OleDbCommandBuilder(DBAdapter)
         Cnn.Open()
         If txtUsername.Text = "" Or txtPwd.Text = "" Or postComboBox.Text = "" Then
             Cnn.Close()
             MsgBox("请填写完整信息!", MsgBoxStyle.Exclamation, "消息框")
         ElseIf Cmd.ExecuteReader.Read Then
             Cnn.Close()
             MsgBox("用户 [ " & txtUsername.Text & " ] 已存在,请重新输入。", vbOKOnly, "系统提示")
         Else
             Cnn.Close()
             Dim response As MsgBoxResult
             response = MsgBox("确定要添加该用户吗?", vbOKCancel + vbQuestion, "系统提示")
             If response = MsgBoxResult.Ok Then                      ' 用户选择“确定”
                '添加用户信息
                AddUser()
                 txtUsername.Clear()
                 txtPwd.Clear()
                 postComboBox.Text = ""
             Else
                 Exit Sub
             End If
         End If
     End Sub

     Sub AddUser()
         Dim SQLString As String
         SQLString = "INSERT www. tb_user VALUES('" & txtUsername.Text & "','" & txtPwd.Text & "','" & postComboBox.Text & "')"
         Cnn.Open()
         Cmd = New OleDbCommand(SQLString, Cnn)
         Cmd.ExecuteNonQuery()
         MessageBox.Show("添加成功!")
         Cnn.Close()
         Cnn.Dispose()
     End Sub
1