查询字符串数据绑定

时间:2012-02-01 10:02:35

标签: c# asp.net

我正在尝试将数据绑定到标签,因为它与我拥有的查询字符串相同。 我一直在说错误。

必须声明标量变量" @ YouthClubID"。

这是我的代码。

      using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data;


namespace WebApplication1
{
    public partial class youthclubpage : System.Web.UI.Page
    {
        int YouthClubID;

       protected void Page_Load(object sender, EventArgs e)
        {
            using (SqlConnection sqlConnection = new SqlConnection("server = sql7.hostinguk.net; uid = joanton7865org7272_phelim; pwd = Armo32nk; database = joanton7865org7272_youthpodcast;"))
            {
                sqlConnection.Open();
                string strSQL = "Select youthclubname from youthclublist WHERE ([YouthClubID] = @YouthClubID)";
                using (SqlCommand sqlCommand = new SqlCommand(strSQL, sqlConnection))
                {
                    sqlCommand.Parameters.AddWithValue("@YouthClubID", Request.QueryString["club"]);
                    txtClubName.Text = sqlCommand.ExecuteScalar() + "";
                }
            }
        }
        }


    }

4 个答案:

答案 0 :(得分:3)

正如其他人所说的那样,错误发生了,因为你没有填充SQL语句中定义的参数。

无论如何,您根本不需要适配器,在您的情况下ExecuteScalar方法就足够了:

using (SqlConnection sqlConnection = new SqlConnection("..."))
{
    sqlConnection.Open();
    string strSQL = "Select youthclubname from youthclublist WHERE ([YouthClubID] = @YouthClubID)";
    using (SqlCommand sqlCommand = new SqlCommand(strSQL, sqlConnection))
    {
        sqlCommand.Parameters.AddWithValue("@YouthClubID", Request.QueryString["club"]);
        txtClubName.Text = sqlCommand.ExecuteScalar() + "";
    }
}

在上面我假设俱乐部ID传递为page.aspx?club=[id here],意味着查询字符串变量名称为“club”,当然更改为真实姓名。

+ ""部分用于放置空文本,以防数据库中没有匹配值 如果没有此类值,sqlCommand.ExecuteScalar()将返回 null - 具有以下简单方式:

txtClubName.Text = sqlCommand.ExecuteScalar().ToString();

在这种情况下会生成错误 - 通过添加空字符串,转换为字符串会自动发生并且看起来更漂亮。另见this question。不用说,如果值不为空,则不会更改。

答案 1 :(得分:0)

您在SQL语句中使用了一个变量,您已在C#代码中定义/设置。

sqlConnection = new SqlConnection(<ConnectionString>);
sqlCommand = new _SQLCli.SqlCommand("Select youthclubname from youthclublist WHERE ([YouthClubID] = @YouthClubID)",sqlConnection);
            sqlCommand.Parameters
                         .Add(new _SQLCli.SqlParameter( "@YouthClubID", <neededValue>);)
            sqlDataAdapter = new SqlDataAdapter(sqlCommand);

答案 2 :(得分:0)

SqlConnection sqlConnection = new SqlConnection("...");
SqlCommand command = new SqlCommand("Select youthclubname from youthclublist WHERE ([YouthClubID] = @YouthClubID)", sqlConnection);
command.Parameters.Add("@YouthClubID", SqlDbType.Int);
command.Parameters["@YouthClubID"].Value = clubID;

SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(command);

答案 3 :(得分:0)

您需要将参数添加到命令对象中。 SqlDataAdapter, 试试这样。如果只从db返回一个值,则可以使用ExecuteScalar方法

  SqlDataAdapter adapter = new SqlDataAdapter();

    // Create the SelectCommand.
    SqlCommand command = new SqlCommand(Select youthclubname from youthclublist WHERE ([YouthClubID] = @YouthClubID)", sqlConnection);

    // Add the parameters for the SelectCommand.
    command.Parameters.AddWithValue("@YouthClubID", yourYouthClubID);
    yourClubName = sqlCommand.ExecuteScalar();
相关问题