我正在使用SQL Server 2014,并且我有一个返回int
值的存储过程:
@ShippingDataID = IDENT_CURRENT ('ShippingData');
如果我查询sql它的工作并且我看到了返回值但是在C#ADO.NET中我总是得到NULL。我尝试了一些我在这里看到但没有任何作用的东西,我很乐意寻求帮助。
这是我的C#代码:
using (con)
{
SqlCommand cmd = new SqlCommand("dbo.spSetShippingData", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter tvpParam = cmd.Parameters.AddWithValue("@tvpNewShippingData", dt);
tvpParam.SqlDbType = SqlDbType.Structured;
cmd.Parameters.Add("@ShippingDataID", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
cmd.ExecuteNonQuery();
val = (int)cmd.Parameters["@ShippingDataID"].Value;
returnValue = new SqlParameter();
returnValue.Direction = ParameterDirection.ReturnValue;
cmd.Parameters.Add(returnValue);
cmd.Parameters.Add("@ShippingDataID", SqlDbType.Int).Direction = ParameterDirection.Output;
val = (int)cmd.Parameters["@ShippingDataID"].Value;
//cmd.ExecuteNonQuery();
}
con.Close();
这是我的SQL代码:
CREATE PROCEDURE [dbo].[spSetShippingData]
@tvpNewShippingData dbo.TVPShippingDataTableType readonly
AS
BEGIN
DECLARE @ShippingDataID INT = 0;
INSERT INTO dbo.ShippingData(FirstName, LastName, Email, Phone, AdditionalPhone, City, Street, StreetNumber, Entrance, ApartmentNumber, PostalCode, CompanyID)
SELECT
FirstName, LastName, Email, Phone, AdditionalPhone,
City, Street, StreetNumber, Entrance,
ApartmentNumber, PostalCode, CompanyID
FROM
@tvpNewShippingData
SET @ShippingDataID = IDENT_CURRENT ('ShippingData');
SELECT @ShippingDataID;
END
答案 0 :(得分:2)
如果要通过返回参数(而不是通过out参数)接收IDENT_CURRENT的值,则需要在存储过程中调用RETURN语句
CREATE PROCEDURE [dbo].[spSetShippingData] @tvpNewShippingData
dbo.TVPShippingDataTableType readonly
AS
BEGIN
INSERT INTO dbo.ShippingData
(FirstName,LastName,Email,Phone,AdditionalPhone,City,
Street,StreetNumber,Entrance,ApartmentNumber,PostalCode,CompanyID)
SELECT FirstName,LastName,Email,Phone,AdditionalPhone,City,
Street,StreetNumber,Entrance,ApartmentNumber,PostalCode,CompanyID
FROM @tvpNewShippingData
RETURN IDENT_CURRENT ('ShippingData');
END
现在您的C#代码应该在ReturnValue参数中接收返回值
cmd.Parameters.Add("@ShippingDataID", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
cmd.ExecuteNonQuery();
val = (int)cmd.Parameters["@ShippingDataID"].Value;
说我不喜欢更简单的方法
CREATE PROCEDURE [dbo].[spSetShippingData] @tvpNewShippingData
dbo.TVPShippingDataTableType readonly
AS
BEGIN
INSERT INTO dbo.ShippingData
(FirstName,LastName,Email,Phone,AdditionalPhone,City,
Street,StreetNumber,Entrance,ApartmentNumber,PostalCode,CompanyID)
SELECT FirstName,LastName,Email,Phone,AdditionalPhone,City,
Street,StreetNumber,Entrance,ApartmentNumber,PostalCode,CompanyID
FROM @tvpNewShippingData
SELECT IDENT_CURRENT ('ShippingData');
END
在你的C#中调用ExecuteScalar而不是ExecuteNonQuery而不使用返回值
object result = cmd.ExecuteScalar();
int val = (result == null ? (int)0 : Convert.ToInt32(result);
ExecuteScalar将返回代码执行的最后一个SELECT的第一行的第一列,因此您不需要构建参数。
答案 1 :(得分:0)
您可以在程序结束时添加
SET @ShippingDataID = IDENT_CURRENT ('ShippingData');
SELECT @ShippingDataID AS RETURN_ID
在c#中
您可以使用DataAdapter获取价值
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(dt);
int shipingId =Convert.ToInt32(dt.Rows[0]["RETURN_ID"].ToString());
我希望你能对这段代码有所了解。