日期:2014-05-18  浏览次数:20831 次

各位大哥!这什么地方做错了?必须声明标量变量@***
C# code

       public static DataSet Search(Entity.WeaponInfo Search)
       {
           SqlConnection conn = new SqlConnection("Server=.; Integrated Security=True; Database=WeaponSalesMng");
           SqlCommand cmd = new SqlCommand("select * from WeapInfo where WeapName=@WeapName", conn);
           cmd.Parameters.Add(new SqlParameter("@WeapName", Search.Name));
           conn.Open();
           DataSet ds = new DataSet();
           SqlDataAdapter da = new SqlDataAdapter(cmd.CommandText, conn);
           da.Fill(ds,"WeapInfo");//在这里提示必须声明标量变量@WeapName
           da.FillSchema(ds, SchemaType.Source, "WeapInfo");
           DataTable table = ds.Tables["WeapInfo"];
           conn.Close();
           return ds;
       }


-----------------------------------------
我将"select * from WeapInfo where WeapName=@WeapName" 换成"select * from WeapInfo where WeapName='XXX'"就可以正常运行
但是Search.Name 确实有值传进来了啊 。。。。
各位老大教教我啊

------解决方案--------------------
SqlDataAdapter da = new SqlDataAdapter(cmd.CommandText, conn);
改成
SqlDataAdapter da = new SqlDataAdapter(cmd);
------解决方案--------------------
探讨

试试
cmd.Parameters.AddWithValue("@WeapName", Search.Name);

------解决方案--------------------
探讨
引用:

SqlDataAdapter da = new SqlDataAdapter(cmd.CommandText, conn);
改成
SqlDataAdapter da = new SqlDataAdapter(cmd);


谢谢你,解决了 为什么我的写法不行呢?