在vb.net中调用子过程

时间:2015-06-17 08:46:01

标签: sql vb.net winforms stored-procedures

只是想问你我是否正在以正确的方式调用vb.net中的子程序我已经测试了它并且它正在工作。我只是想知道我是否需要改变编码方式?

所以这是我的Sub程序:

Sub loadStudDB()

 'To load student info from database and pass it through objecs from winform'

    Try
        con.Open()
        Dim studid As Integer = Val(Form1.studentidtxt.Text)
        Dim selectQ = "SELECT * FROM StudentInfoTBL WHERE StudentID=@studid"
        Dim cmd As New SqlCommand(selectQ, con)

        cmd.Parameters.AddWithValue("@studid", studid)
        Dim dr As SqlDataReader = cmd.ExecuteReader()
        If dr.HasRows Then
            dr.Read()

            studidtxt.Text = dr.Item("StudentID").ToString
            studname.Text = dr.Item("StudentName")
            agebox.Text = dr.Item("Age").ToString

            If dr.Item("Gender") = "Male" Then
                malebtn.Checked = True
            Else
                femalebtn.Checked = True
            End If

            dobtxt.Text = dr.Item("DOB")
            homeaddtxt.Text = dr.Item("Home Address")
            citytxt.Text = dr.Item("City").ToString
            telephonetxt.Text = dr.Item("Telephone")
            mobiletxt.Text = dr.Item("Mobile")
            emailtxt.Text = dr.Item("Email")

            dr.Close()
            con.Close()

        End If
    Catch ex As Exception
        MessageBox.Show(ex.ToString)
    Finally
        con.Close()
    End Try
End Sub

然后我将从表单加载事件中调用它:

Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
     'call procedure created'
        loadStudDB()
    End Sub

0 个答案:

没有答案