sql-server-2005 – 在SQL Server 2005中向存储过程添加参数之间
我想知道这两个符号之间的区别. 首先,我有一个存储过程 CREATE PROCEDURE AddSomething( @zonename varchar(50),@desc varchar(255),@TheNewId int OUTPUT ) AS BEGIN INSERT INTO a_zone(zonename,descr) VALUES(@zonename,@desc) SELECT @TheNewId = SCOPE_IDENTITY() END 如果我以这种方式添加参数有什么区别 SqlCommand Cmd = new SqlCommand("AddSomething",oConn); Cmd.CommandType = CommandType.StoredProcedure; SqlParameter oParam1 = Cmd.Parameters.AddWithValue("@zonename",sName); SqlParameter oParam2 = Cmd.Parameters.AddWithValue("@desc",description); 和 SqlCommand Cmd2 = new SqlCommand("AddSomething",oConn); Cmd2.CommandType = CommandType.StoredProcedure; cmd2.Parameters.Add("@zonename",SqlDbType.VarChar).Value = zonename.Text.Trim(); cmd2.Parameters.Add("@desc",SqlDbType.VarChar).Value = desc.Text.Trim(); 请帮帮我 谢谢你的期待 解决方法以下是一些解释:difference between command Add and AddWithValue Dim cmd as new SqlCommand("SELECT * FROM MyTable WHERE MyDate>@TheDate",conn) cmd.Parameters.Add("@TheDate",SqlDbType.DateTime).Value="2/1/2007" VS cmd.Parameters.AddWithValue("@TheDate","2/1/2007") “当进入参数时,Add强制从字符串到日期的转换.AddWithValue只是将字符串传递给SQL Server.
> http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters.aspx 编辑: 获取输出参数的示例: C# cmd.Parameters.Add(new SqlParameter("@TheNewId",SqlDbType.Int,int.MaxValue)); cmd.Parameters("@TheNewId").Direction = ParameterDirection.Output; cmd.ExecuteNonQuery(); int theNewID = (int)cmd.Parameters("@TheNewId").Value; VB.Net cmd.Parameters.Add(New SqlParameter("@TheNewId",Int32.MaxValue)) cmd.Parameters("@TheNewId").Direction = ParameterDirection.Output cmd.ExecuteNonQuery() Dim theNewID As Int32 = DirectCast(cmd.Parameters("@TheNewId").Value,Int32) (编辑:航空爱好网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |