从C#中的存储过程返回行id?

时间:2017-05-22 20:59:34

标签: c# sql-server stored-procedures

我试图获取插入行的行ID。

这是Fuel类:

public class Fuel
{
    private Int32 fuelId;
    private DateTime fuelDate;

    public Fuel(DateTime fuelDate)
    {
        this.fuelDate = fuelDate;
    }

    public Fuel(Int32 fuelId, DateTime fuelDate)
    {
        this.fuelId = fuelId;
        this.fuelDate = fuelDate;
    }

    public Int32 FuelId() { return fuelId; }
    public DateTime FuelDate() { return fuelDate; }
}

这是尝试写入数据库的方法:

protected String connectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;
        AttachDbFilename=" + "C:\\Users\\keith_000\\Documents\\Visual Studio 2017\\Projects\\RubberDress\\RubberDress\\Rubber.mdf" + ";" +
        "Integrated Security=True;" +
        "Connect Timeout=30";

public Int32 InsertFuelPurchase(Fuel myAddFuel)
{
        Int32 myFuelId;

        using (SqlConnection mySqlConn = new SqlConnection(connectionString))
        {
            mySqlConn.Open();

            using (SqlCommand myComm = new SqlCommand("FUELINSERT", mySqlConn))
            {
                myComm.CommandType = CommandType.StoredProcedure;
                myComm.Parameters.Add("@FUELDATE", SqlDbType.DateTime).Value = myAddFuel.FuelDate();
                myComm.Parameters.Add("@FUELID", SqlDbType.Int).Direction = ParameterDirection.Output;
                myFuelId = Convert.ToInt32(myComm.Parameters["@FUELID"].Value);
                mySqlConn.Close();
            }
        }

        return myFuelId;
    }

存储过程是

CREATE PROCEDURE [dbo].[FUELINSERT]
    @FUELDATE DATETIME,
    @FUELID INT OUTPUT
AS
    INSERT INTO FUELPURCHASE (FUELPURCHASEDATE)  
    VALUES (@FUELDATE)

    SELECT @FUELID = SCOPE_IDENTITY()

写于下表

CREATE TABLE [dbo].[FUELPURCHASE] 
(
    [FUELPURCHASEID] INT IDENTITY (1, 1) NOT NULL,
    [FUELPURCHASEDATE] DATETIME NOT NULL

    PRIMARY KEY CLUSTERED ([FUELPURCHASEID] ASC)
);

目前返回的结果为0。

当我调试方法时,我发现它是从存储过程返回的。

有人能找到解决问题的地方吗?

3 个答案:

答案 0 :(得分:3)

当然你需要执行命令,( ExecuteScalar ExecuteNonQuery ,甚至 ExecuteReader 将调用SP并运行你的SqlCommand)但是你不需要有一个out参数,只需要在存储过程中选择SELECT SCOPE_IDENTITY()并用ExecuteScalar执行命令

CREATE PROCEDURE [dbo].[FUELINSERT]
    @FUELDATE DATETIME
AS
    INSERT INTO FUELPURCHASE (FUELPURCHASEDATE) VALUES (@FUELDATE)
    SELECT SCOPE_IDENTITY()

现在您可以使用

调用SP
....
using (SqlCommand myComm = new SqlCommand("FUELINSERT", mySqlConn))
{
    myComm.CommandType = CommandType.StoredProcedure;
    myComm.Parameters.Add("@FUELDATE", SqlDbType.DateTime).Value = myAddFuel.FuelDate();
    myFuelId = Convert.ToInt32(myComm.ExecuteScalar());
}
....

答案 1 :(得分:0)

您缺少myComm.ExecuteNonQuery(); ...您的代码应如下所示:

myComm.ExecuteNonQuery();
myFuelId = Convert.ToInt32(myComm.Parameters["@FUELID"].Value);

答案 2 :(得分:0)

试试这个:

        mySqlConn.Open();
        using (SqlCommand myComm = mySqlConn.CreateCommand())
        {
            myComm.CommandType = CommandType.StoredProcedure;
            myComm.CommandText = "Exec YourProcedure @FUELDATE,@FUELID";
            myComm.Parameters.Add("@FUELDATE", SqlDbType.DateTime).Value = myAddFuel.FuelDate();
            myComm.Parameters.Add("@FUELID", SqlDbType.Int).Direction = ParameterDirection.Output;
            myFuelId = Convert.ToInt32(myComm.Parameters["@FUELID"].Value);
            int result = myComm.ExecuteNonQuery();

            mySqlConn.Close();
            return result;

        }

返回单个值时使用myComm.ExecuteScalar()

取决于您的FUELDATE,FUELID位置: