标题:无法向数据库添加数据问题,求教!!
只看楼主
liuyu123407
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2014-6-4
 问题点数:0 回复次数:1 
无法向数据库添加数据问题,求教!!
我的程序显示添加数据成功,可是数据库里却没有显示。
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If TextBox1.Text = "" Then
            MsgBox("学号不能为空")
        ElseIf TextBox6.Text = "" Then
            MsgBox("入学时间不能为空")
        ElseIf TextBox7.Text = "" Then
            MsgBox("学制不能为空")

        Else
            singleYanzheng()

        End If
    End Sub
    Sub singleYanzheng()
        Dim str As String

        str = "Data Source=7NNPKL9QYGFWVM2\SQLEXPRESS;Initial Catalog = Student;integrated Security=true"
        Dim con As New SqlConnection(str)
        con.Open()
        Dim sql As String = "select * from student_Info where student_ID='" & TextBox1.Text.ToString().Trim() & "' "

        Dim cmd As New SqlCommand(sql, con)
        Dim reader As SqlDataReader
        reader = cmd.ExecuteReader

        If reader.Read() = True Then



            MsgBox("该学号已存在")


        Else
            addStuInf()
            Dim p As Integer = MsgBox("添加成功!" + vbCrLf + "是否继续添加?", MsgBoxStyle.YesNo)
            If p = 7 Then
                Me.Hide()
            Else
                clear()
            End If

        End If
    End Sub
    Sub addStuInf()
        Dim str As String

        Dim sex As String
        If RadioButton1.Checked Then
            sex = "男"
        ElseIf RadioButton2.Checked Then
            sex = "女"
        Else
            sex = ""
        End If
        str = "Data Source=7NNPKL9QYGFWVM2\SQLEXPRESS;Initial Catalog = Student;integrated Security=true"
        Dim con As New SqlConnection(str)
        con.Open()

        Dim sql As String = "insert into student_Info (student_ID,student_Name,student_Sex,born_Date,depart_ID,class_NO,tele_Number,entr_Date,stu_Year,edu_bg,address,codeNo,zzmm,comment) values('" & TextBox1.Text.ToString().Trim() & "','" & TextBox2.Text.ToString().Trim() & " ','" & sex & " ','" & TextBox3.Text.ToString.Trim() & "','" & TextBox12.Text.ToString.Trim() & "','" & TextBox4.Text.ToString.Trim() & "','" & TextBox5.Text.ToString.Trim() & "','" & TextBox6.Text.ToString.Trim() & "','" & TextBox7.Text.ToString.Trim() & "','" & TextBox8.Text.ToString.Trim() & "','" & TextBox9.Text.ToString.Trim() & "','" & TextBox10.Text.ToString.Trim() & "','" & ComboBox1.Text.ToString.Trim() & "','" & TextBox11.Text.ToString.Trim() & "') "

        Dim cmd As New SqlCommand(sql, con)

        Try

            cmd.ExecuteNonQuery()

        Catch e As Exception

            Console.WriteLine(e.Message)

        End Try

        Console.WriteLine("Record Add")
    End Sub

    Private Sub RadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged
        Dim sex As String
        If RadioButton1.Checked Then
            sex = "男"
        ElseIf RadioButton2.Checked Then
            sex = "女"
        Else
            sex = ""

        End If

    End Sub
搜索更多相关主题的帖子: 数据库 
2014-06-04 11:59
wwf3045
Rank: 2
等 级:论坛游民
威 望:1
帖 子:25
专家分:69
注 册:2010-8-25
得分:0 
cmd.ExecuteNonQuery()后面添加一句con.close
2014-09-30 22:34



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




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

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