【问题标题】:Sql Conversion from nVarchar to int error从 nVarchar 到 int 的 Sql 转换错误
【发布时间】:2013-11-29 05:12:17
【问题描述】:

错误:

将 nvarchar 值“select TopicID from Topic where TopicName='Data Structure'”转换为数据类型 int 时转换失败

代码:

public void  BindGridview()
{
    string strConnString = ConfigurationManager.ConnectionStrings["infoConnectionString"].ConnectionString;

    SqlConnection sqlcon = new SqlConnection(strConnString);
    sqlcon.Open();

    string strquery2 = "select TopicID from Topic where TopicName='" + ddltopic.SelectedItem.Text+ "'";

    string strquery3 = "select i.name ,i.score from info as i,Topic as t where  i.topic_id=@topicid";
    SqlCommand cmd = new SqlCommand(strquery3,sqlcon);
    cmd.Parameters.AddWithValue("@topicid",strquery2);

    cmd.Connection = sqlcon;

    SqlDataReader dr;;

    this.GridView1.DataSource =cmd.ExecuteReader();
    this.GridView1.DataBind();

    sqlcon.Close();
    }
}

谁能告诉我哪里出错了?任何帮助将不胜感激..请尽快回复..提前谢谢..

【问题讨论】:

  • TopicID 的数据类型是什么?
  • @user3048066 :如果你想获取 TopicID 值并将其传递给其他查询存储过程,我认为这很好。
  • Bad habits to kick : using old-style JOINs - 旧式 逗号分隔的表格列表 样式已随 ANSI-92 SQL 标准(超过 20 年前)
  • @user3048066: 请尝试创建可读查询..

标签: c# sql sql-server


【解决方案1】:

您传递的是整个查询而不是此行中的主题 id

cmd.Parameters.AddWithValue("@topicid",strquery2);

然后将其作为参数并将其添加到以下查询中。如果这是一个子查询,您总是可以先执行它,然后在参数中使用结果。

但它失败的原因是因为您实际上是在尝试通过传入这样的查询字符串来比较 Stringint

【讨论】:

    【解决方案2】:

    我认为你想要什么 cmd.Parameters.AddWithValue("@topicid",strquery2);是strquery2返回的值??? , 如果您先执行此查询,将生成主题 ID,并且将使用此结果而不是查询本身

    这就是你想要的吗??

    【讨论】:

    • 非常感谢Naresh先生的帮助..是的,这是正确的..你真的很摇滚!!!我被这个问题困了三天..非常感谢您的帮助..谢谢阿顿!!
    • @NareshPansuriya 先生,现在我在我的 gridview 中获得了多个值.. 我只需要对应的值.. 例如学生姓名是:a 和 b,得分为 8 和 9 ..但他们在gridview中显示的值不止一个..您能帮忙...提前谢谢!!!
    • 该问题与当前问题有关吗?
    • 是的@naresh 先生,这是相关的,但现在它正在工作。,所以感谢大家的快速响应和帮助我,谢谢 aton,!!
    【解决方案3】:

    你能用下面的代码试试吗,我没有测试过,但它应该适合你

    public void  BindGridview()
    {
        string strConnString = ConfigurationManager.ConnectionStrings["infoConnectionString"].ConnectionString;
                    SqlConnection sqlcon = new SqlConnection(strConnString);
                    sqlcon.Open();
                    //Equal is not working when subquery return more records
                    string strquery2 = "select i.name ,i.score from info as i,Topic as t where  i.topic_id in (select TopicID from Topic where TopicName=@TopicName)";
    
                    SqlCommand cmd = new SqlCommand(strquery2, sqlcon);
                    cmd.Parameters.AddWithValue("@TopicName", ddltopic.SelectedItem.Text);
                    cmd.Connection = sqlcon;
                    SqlDataReader dr; ;
    
                    this.GridView1.DataSource =cmd.ExecuteReader();
                    this.GridView1.DataBind();
                    sqlcon.Close();
    }
    

    【讨论】:

    • :答案还可以。但是当我们在 SQL 查询中使用 in 时,性能会降低。
    【解决方案4】:

    不是一个实际的答案,但 cmets 太短了。

    此代码易受SQL injection

    string strquery2 = "select TopicID from Topic where TopicName='" + ddltopic.SelectedItem.Text+ "'";
    

    想象一下,在未来的某个时刻,某个人(您或正在修改您的代码的其他人)决定用组合框替换下拉列表?现在想象有人在组合框中输入了这个文本:

    '; TRUNCATE TABLE Topic; --'
    

    现在您的 SQL 服务器将执行此操作:

    select TopicID from Topic where TopicName = '';
    TRUNCATE TABLE Topic; --'
    

    Learn 使用parameters

    【讨论】:

      猜你喜欢
      • 2014-10-13
      • 2011-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-22
      • 2011-04-16
      相关资源
      最近更新 更多