必须在参数化查询中声明标量变量“@user_code”

时间:2012-10-04 06:49:45

标签: asp.net vb.net parameterized

受保护的子Page_Load(ByVal sender As Object,ByVal e As System.EventArgs)处理Me.Load

    Dim ds As DataSet = New DataSet()
    Dim param As SqlParameter = New SqlParameter("@user_code", SqlDbType.Char, 4)
    param.Value = "0016"
    Const sqlstr As String = "select sectors.sector_code,sector_name   from user_sectors inner join sectors on user_sectors.sector_code = sectors.sector_code  where user_code = @user_code  and sectors.sector_code not in ('z')"

    Dim da As SqlDataAdapter = New SqlDataAdapter(sqlstr, _con)
    da.Fill(ds)
    ddl.DataValueField = "sector_code"
    ddl.DataTextField = "sector_name"
    ddl.DataSource = ds.Tables(0)
    ddl.DataBind()

End Sub

1 个答案:

答案 0 :(得分:2)

向dataadapter添加参数的代码

    SqlConnection conn = new SqlConnection("Data 
             Source=localhost;Database=Northwind;Integrated Security=SSPI");
  SqlCommand command = new SqlCommand("GetProducts", conn);
  command.CommandType = CommandType.StoredProcedure;
  command.Parameters.Add("@CategoryID", SqlDbType.Int).Value = 1;
  SqlDataAdapter adapter = new SqlDataAdapter(command);
  DataSet ds = new DataSet();
  adapter.Fill(ds, "Products");

向您的数据适配器对象添加参数

da.Parameters.Add(param)

你的代码中缺少这个

MSDN:Using Parameters with a DataAdapter