在SQL Server Management Studio中:
致电代码:
DECLARE @return_value int,
@CustomerID bigint
EXEC @return_value = [dbo].[InsertCustomer]
@CustomerID = @CustomerID OUTPUT,
@Name = N'CustomerName'
SELECT @CustomerID as N'@CustomerID'
SELECT 'Return Value' = @return_value
在应用程序代码中:
CustomerID
的返回值始终为1 代码:
ObjectParameter ob = new ObjectParameter("CustomerID", typeof(long));
var CustomerID = db.InsertCustomer(ob, "CustomerName");
我试图通过传递一个类型并经常按值传递来更改新的ObjectParameter(,)
第二个参数(例如:0,1等),但没有用。
这里可能出了什么问题?
更新:
这是Entity Framework过程代码的样子:
public virtual int InsertCustomer(ObjectParameter customerID, string name)
{
var nameParameter = name != null ?
new ObjectParameter("Name", name) :
new ObjectParameter("Name", typeof(string));
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("InsertCustomer", customerID, nameParameter);
}
答案 0 :(得分:2)
直接ADO :
请参阅此link.。您需要在proc中指定输出参数,并且需要在调用代码中指定它。
SqlParameter outParam = new SqlParameter("CustomerID", 0);
using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["StringName"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("InsertCustomer", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = CustomerName;
cmd.Parameters.Add(outParam.ParameterName, SqlDbType.BigInt).Direction = ParameterDirection.Output;
con.Open();
var CustomerID = Convert.ToInt64(cmd.Parameters["CustomerID"].Value);
}
}
实体框架:
请参阅此link
ObjectParameter ob = new ObjectParameter("CustomerID", 0);
// Wrong
var CustomerID = db.InsertCustomer(ob, "CustomerName");
// Right
db.InsertCustomer(ob, "CustomerName");
var CustomerID = (long)ob.Value;