当前位置: 代码迷 >> C# >> 参数化查询,未提供参数有关问题
  详细解决方案

参数化查询,未提供参数有关问题

热度:131   发布时间:2016-05-05 05:17:02.0
参数化查询,未提供参数问题
本帖最后由 qq_24981117 于 2015-01-02 13:25:05 编辑
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "jrgwc")
        {
            
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=bysj;Integrated Security=True");
            con.Open();
            SqlParameter[] param = new SqlParameter[] { new SqlParameter("@spbh", SqlDbType.Int) };
            String dingdan = "insert into 订单副表(spbh) Select spbh From 商品信息表 Where 订单副表.spbh = @spbh";
            SqlCommand sqlCom = new SqlCommand(dingdan, con);
            new SqlParameter("@spbh", param[0].Value);
            foreach (SqlParameter item in param)
            {
                sqlCom.Parameters.Add(item);
            }
            sqlCom.ExecuteNonQuery();
      }
}
这段的错误提示是:参数化查询 '(@spbh int)insert into 订单副表(spbh) Select spbh From 商品信息表 Where 订' 需要参数 @spbh,但未提供该参数。
这个怎么改啊?
谢谢大家
------解决思路----------------------
引用:
那该怎么写啊,虽然直接给它赋值能用,但是我需要的是把商品信息表的spbh的值取出来写到订单副表里面

先看看基础知识!

param[0].Value = xxx;
先赋个值
------解决思路----------------------
http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlparameter(v=vs.110).aspx
------解决思路----------------------
new SqlParameter("@spbh", param[0].Value);

[email protected]

@是在参数化字符串中起占位符作用,@本身不是参数名称的一部分,后面的spbh才是参数名称。
------解决思路----------------------
            using(SqlConnection conn=new SqlConnection(connstr))
            {
                conn.Open();
                using(SqlCommand cmd=conn.CreateCommand())
                {
                    string name = Console.ReadLine();
                    string password = Console.ReadLine();
                    //推荐使用
                    cmd.CommandText = "select count(*) from users where [email protected] and [email protected]";
                    cmd.Parameters.Add(new SqlParameter("name", name));
                    cmd.Parameters.Add(new SqlParameter("wd", password));
                    //------------
                    int count=(int)cmd.ExecuteScalar();

                    Console.WriteLine(count);
                }
            }

推荐这样写,原来的那段代码我都看迷糊了,越看越模糊
  相关解决方案