Sql参数集已过时使用Addwithvalue()这是什么错误我为什么要解决它? [英] Sql Parameter Collection Obsolete Use Addwithvalue() What Is This Error Why Am I Getting How To Solve It ?

查看:145
本文介绍了Sql参数集已过时使用Addwithvalue()这是什么错误我为什么要解决它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public DataSet FindRecord(int RollNumber)
        {
            using (SqlConnection con = new SqlConnection(cs))
            {
                    SqlCommand cmd = new SqlCommand("spfindtblstudent", con);
                    cmd.Parameters.Add("@RollNumber", RollNumber);//
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlDataAdapter da = new SqlDataAdapter("cmd", con);
                    con.Open();
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    con.Close();
                    return ds;
                    
                
            }
        }





错误我得到的是这里



the error iam getting is Here

cmd.Parameters.Add("@RollNumber", RollNumber);//



同时为参数添加值提到错误提到如何摆脱我已经使用了Addwithparameters但没有弄清楚



帮助我


while adding values to parameters getting the error mentioned how to get rid of that i have used the Addwithparameters but not getting it correct

Help me

推荐答案

替换 cmd.Parameters.Add(@ RollNumber,RollNumber);



replace cmd.Parameters.Add("@RollNumber", RollNumber);
to
cmd.Parameters.AddWithValue("@RollNumber", RollNumber);




来自MSDN的




from MSDN

Quote:

使用SqlParameterCol的这个重载时要小心lection.Add方法指定整数参数值。因为此重载采用Object类型的值,所以当值为零时,必须将整数值转换为Object类型,如下面的C#示例所示。

parameters.Add (@ pname,Convert.ToInt32(0));

如果不执行此转换,编译器会假定您正在尝试调用SqlParameterCollection.Add (字符串,SqlDbType)重载。

Use caution when you are using this overload of the SqlParameterCollection.Add method to specify integer parameter values. Because this overload takes a value of type Object, you must convert the integral value to an Object type when the value is zero, as the following C# example demonstrates.
parameters.Add("@pname", Convert.ToInt32(0));
If you do not perform this conversion, the compiler assumes that you are trying to call the SqlParameterCollection.Add (string, SqlDbType) overload.


这不是错误 - 这是一个警告。但是它说的是,参数.Add方法是折旧的 - 有一个更现代,更好的版本 - 你应该使用它。从文档的最新版本开始,Add方法已过时,这意味着可以从框架的未来版本中删除它。因此,你不应该在新代码中使用它。



SqlParameter.Add通常只在需要更多控制正在发生的事情时使用,但是这是更长的代码。在这种情况下,它只是一个完整性的东西:

It's not an error - it's a warning. But what it is saying is that the Parameters.Add method is depreciated - there is a more modern, better version - and that you should use that instead. As of the latest version of the documentation, the Add method is obsolete, which means that it could be removed from future versions of the framework. You shouldn't use it in new code as a result.

SqlParameter.Add is normally only used when you need a lot more control over what is going on, but it's a lot longer code. In this case, It's just a "completeness" thing:
cmd.Parameters.AddWithValue("@RollNumber", RollNumber);

将修复它。


这篇关于Sql参数集已过时使用Addwithvalue()这是什么错误我为什么要解决它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆