无法使用vb从asp.net中的数据库中读取数据

时间:2017-10-27 15:34:08

标签: asp.net vb.net

我正在使用vb学习asp.net,我正在完成大学任务 我必须从msaccess读取数据并在标签中显示记录 基本上我在做'搜索'操作

这是代码

公共类WebForm2     继承System.Web.UI.Page     昏暗的连接作为新的OleDb.OleDbConnection     昏暗的sname,lname,city,subj,性别,爱好,宗教As String     昏暗的年龄,电话为整数

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


    connection = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\himzz\Documents\Database1.accdb;")
    connection.Open()

    If Not IsPostBack Then


    End If
End Sub

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


    sname = TextBox1.Text
    lname = TextBox2.Text
    age = TextBox3.Text
    city = TextBox4.Text
    phone = TextBox5.Text
    subj = DropDownList1.SelectedValue

    If RadioButton1.Checked Then
        gender = "male"
    ElseIf RadioButton2.Checked Then
        gender = "female"
    Else
        gender = "other"
    End If

    'If CheckBox1.Checked And CheckBox2.Checked And CheckBox3.Checked Then
    '    hobbies = "cricket swimming tennis "
    'End If

    If CheckBox1.Checked Then
        hobbies += "cricket "
    End If

    If CheckBox2.Checked Then
        hobbies += "swimming "
    End If

    If CheckBox3.Checked Then
        hobbies += " tennis "
    End If

    religion = DropDownList2.SelectedValue



    Try
        Dim q As String = "insert into stud (sname,lname,age,city,phone,subj,gender,hobbies,religion) values('" & sname & "','" & lname & "','" & age & "','" & city & "','" & phone & "','" & subj & "','" & gender & "','" & hobbies & "','" & religion & "' )"
        Dim cmd As New OleDb.OleDbCommand(q, connection)
        cmd.ExecuteNonQuery()
        MsgBox("submitted")

    Catch ex As Exception
        MsgBox("error")
    End Try
    Response.Redirect(Request.Url.AbsoluteUri)

End Sub

Protected Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click



    Dim q As String = "select lname from stud where sname='" & sname & "' "
    Dim cmd As New OleDb.OleDbCommand(q, connection)
    Dim reader As OleDb.OleDbDataReader
    reader = cmd.ExecuteReader()




    While reader.Read()
(tried this but not worked)  ' Label12.Text = " " & reader("sname")

(试过这个,但没有奏效)' Label12.Text = reader.Item(" sname")。ToString() (不工作)Label12.Text = reader(" sname")。ToString

        'Label13.Text = "age " & reader("age")
        'Label14.Text = "city " & reader("city")
        'Label15.Text = "phone no " & reader("phone")
        'Label16.Text = "subject " & reader("subj")
        'Label17.Text = "Gedner " & reader("gender")
        'Label18.Text = "Hobbies " & reader("hobbies")
        'Label19.Text = "Religion " & reader("religion")


    End While

    reader.Close()
End Sub

结束班

0 个答案:

没有答案