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

无法向数据库添加数据问题,求教!!

liuyu123407 发布于 2014-06-04 11:59, 1503 次点击
我的程序显示添加数据成功,可是数据库里却没有显示。
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
1 回复
#2
wwf30452014-09-30 22:34
cmd.ExecuteNonQuery()后面添加一句con.close
1