SqlException捕获错误 [英] SqlException caught error
本文介绍了SqlException捕获错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请问我正在设计用于学习目的的演示航空公司网站上是否有错误.该错误必须声明标量变量"@Dep"
下面是我的代码,用于使用用户选择的值来显示数据库的结果.被抓到
Please i am having an error from my demo airline website i am designing for learning purpose.The error is must declare the scalar variable "@Dep"
Below is my code to use user selected values to display result from a database. was caught
Protected Sub buttFindFlight_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles buttFindFlight.Click
Try
Dim conn As New SqlConnection
Dim cmd As New SqlCommand
Dim adp As New SqlDataAdapter
Dim ds As New DataSet
conn.ConnectionString = ConfigurationManager.ConnectionStrings("AirportReservationSystemConnectionString").ConnectionString
cmd.Connection = conn
conn.Open()
''cmd.CommandType = CommandType.Text
adp = New SqlDataAdapter("select LeavPlace as Departing, DestPlace As Destination FROM DailyFlightInfo where LeavPlace =@Dep And DestPlace =@Dest ", conn)
cmd.Parameters.Add(New SqlParameter("@Dep", SqlDbType.VarChar, 50))
cmd.Parameters.Add(New SqlParameter("@Dest", SqlDbType.VarChar, 50))
cmd.Parameters("@Dep").Value = drpLeaving.SelectedItem.Value
cmd.Parameters("@Dest").Value = drpGoing.SelectedItem.Value
adp.Fill(ds)
gridView.DataSource = ds
gridView.DataBind()
推荐答案
@Dep 代表什么?
您是否要使用参数化查询.
如果是这样,请使用sqlcommand而不是SqlDataAdapter.这是一个示例 [
What does @Dep stands for?
Are you trying to use parameterized query.
If so, use sqlcommand instead of SqlDataAdapter. Here is an Example[^]
Regards
Sebastian
我找不到您的命令和dataAdapter之间的连接,因此请尝试以下操作:
I can''t find connection between your command and dataAdapter therefore try this:
'...
cmd.CommandText = "select LeavPlace as Departing, DestPlace As Destination FROM DailyFlightInfo where LeavPlace =@Dep And DestPlace =@Dest "
cmd.Parameters.Add(New SqlParameter("@Dep", SqlDbType.VarChar, 50))
cmd.Parameters.Add(New SqlParameter("@Dest", SqlDbType.VarChar, 50))
cmd.Parameters("@Dep").Value = drpLeaving.SelectedItem.Value
cmd.Parameters("@Dest").Value = drpGoing.SelectedItem.Value
adp = New SqlDataAdapter(cmd);
adp.Fill(ds)
gridView.DataSource = ds
gridView.DataBind()
'...
这篇关于SqlException捕获错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文