使用VB从数据库中检索数据

时间:2014-10-01 23:43:56

标签: vb.net

我有一个 ASP日历然后我打算做的是,当用户点击日历中的日期时,它会更新下面的gridview以显示该日期的日程安排,但是代码抛出了 Catch ex As Exception

    Protected Sub calSched_SelectionChanged(sender As Object, e As EventArgs) Handles calSched.SelectionChanged 'Selecting a date
    schedule.Value = calSched.SelectedDate.ToShortDateString()

    Try
        con = New MySqlConnection("Server=localhost;Database=;Uid=root;Pwd=;")
        Using cmd = New MySqlCommand("SELECT sched_id, sched_date, sched_time, dent_lname FROM schedule_table INNER JOIN dentist_table WHERE sched_date = @p1", con)
            cmd.Parameters.AddWithValue("@p1", schedule.Value.ToString)
            con.Open()
            dat = cmd.ExecuteReader()
            Dim ds As New DataSet()
            Dim a As New MySqlDataAdapter(cmd)
            a.Fill(ds)
            grdSched.DataSource = ds
            While (dat.Read())
                grdSched.DataBind()
            End While
            con.Close()
        End Using
    Catch ex As Exception
        lblMessage.Text = "Cannot recover schedule"
    End Try

End Sub

Schedule.value.tostring 是一个输入字段,可在用户点击时更新任何建议吗?

Schedule_table - sched_id(主键,自动增量),sched_date(VARCHAR 25),sched_time(VARCHAR 9),dent_id(INT 6,无符号Zerofill,外键)

Dentist_table - dent_id(主键,自动增量,Int 6,无符号zerofill),dent_fname,dent_lname

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

Protected Sub calSched_SelectionChanged(sender As Object, e As EventArgs) Handles calSched.SelectionChanged 'Selecting a date
 schedule.Value = calSched.SelectedDate.ToShortDateString()

 Try
    con = New MySqlConnection("Server=localhost;Database=;Uid=root;Pwd=;")
    Using cmd = New MySqlCommand("SELECT sched_id, sched_date, sched_time, dent_lname FROM schedule_table WHERE sched_date = @p1", con)
        cmd.Parameters.AddWithValue("@p1", schedule.Value.ToString)
        con.Open()
        Dim ds As New DataSet()
        Dim a As New MySqlDataAdapter(cmd)
        a.Fill(ds)
        grdSched.DataSource = ds.Tables(0)
        grdSched.DataBind()
    End Using
 Catch ex As Exception
    lblMessage.Text = "Cannot recover schedule"
 Finally
    con.Close() 'this block will always run, close it here error or not
 End Try

End Sub