C# return 来自存储过程的双精度值

C# return double value from stored procedure

我想 return 来自存储过程的 double 值,这样我就可以从表单中调用它并将其值乘以文本框的值。

我的存储过程如下所示:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[Get_Weight]
    @ID INT,
    @Weight FLOAT OUTPUT
AS
    SELECT @Weight = [Weight] 
    FROM [Item]
    WHERE ID_Item = @ID

这是我的数据访问层class:

class DataAccessLayer
{
    SqlConnection sqlconnection;

    public DataAccessLayer()
    {
        PL.FRM_LOGIN frm = new PL.FRM_LOGIN();
        sqlconnection = new SqlConnection("Server='"+System.Environment.MachineName+"';DataBase='"+frm.txtDataBase.Text+"';Integrated Security=true");          
    }

    // Method to open the connection
    public void Open()
    {
        if(sqlconnection.State != ConnectionState.Open)
        {
            sqlconnection.Open();
        }
    }

    // Method to close the connection
    public void Close()
    {
        if(sqlconnection.State == ConnectionState.Open)
        {
            sqlconnection.Close();
        }
    }

    // Method to read data from database
    public DataTable SelectData(string stored_procedure, SqlParameter[] param)
    {
        SqlCommand sqlcmd = new SqlCommand();
        sqlcmd.CommandType = CommandType.StoredProcedure;
        sqlcmd.CommandText = stored_procedure;
        sqlcmd.Connection = sqlconnection;

        if (param != null)
        {
            sqlcmd.Parameters.AddRange(param);
        }

        SqlDataAdapter da = new SqlDataAdapter(sqlcmd);
        DataTable dt = new DataTable();
        da.Fill(dt);

        return dt;
    }

    // Method to insert, update and delete data from database
    public void ExecuteCommand(string stored_procedure, SqlParameter[] param)
    {
        SqlCommand sqlcmd = new SqlCommand();
        sqlcmd.CommandType = CommandType.StoredProcedure;
        sqlcmd.CommandText = stored_procedure;
        sqlcmd.Connection = sqlconnection;

        if (param != null)
        {         
            sqlcmd.Parameters.AddRange(param);           
        }

        sqlcmd.ExecuteNonQuery();
    }
}

我想在业务层中创建一个 class 的方法,该方法可以 return 值 - 例如

public void Get_Weight(int ID, double UWeight)
{
    DAL.DataAccessLayer DAL = new DAL.DataAccessLayer();
    DAL.Open();

    SqlParameter[] param = new SqlParameter[2];

    param[0] = new SqlParameter("@ID", SqlDbType.Int);
    param[0].Value = ID;

    param[1] = new SqlParameter("@Weight", SqlDbType.Float);
    param[1].Value = UWeight;
    param[1].Direction = ParameterDirection.Output;

    DAL.ExecuteCommand("Get_Weight", param);
    DAL.Close();
}

然后,我从表单中调用该方法

void CalculateWeight()
{           
    if (txtLength.Text != string.Empty && cmbName.Text != null)
    {
        txtWeight.Text = (Convert.ToInt32(txtLength.Text) *(//the code)).ToString();
    }
}

请帮帮我

使用 ExecuteScalar 从存储过程中获取值

public double ExcuteCommande(string stored_procedure,SqlParameter[] param)
{
    SqlCommand sqlcmd = new SqlCommand();
    sqlcmd.CommandType = CommandType.StoredProcedure;
    sqlcmd.CommandText = stored_procedure;
    sqlcmd.Connection = sqlconnection;
    if (param!=null)
    {         
            sqlcmd.Parameters.AddRange(param);           
    }
    var back=sqlcmd.ExecuteScalar();
    double result;
    double.TryParse(back.ToString(), out result);
    return result;
}

如果这是一次代码审查,我会对您的数据访问层方法产生一些疑问,但为了解决您眼前的问题,我建议您将 Get_Weight 方法更改为 return 一个 double 而不是 UWeight 参数。由于您的 OUTPUT 参数仅被设置而未用作输入,因此您可以为其指定值 DBNull.Value。最后,看起来您的程序中可能有错字,列名真的是 "Wight" 吗?

public double Get_Weight(int ID)
{
    DAL.DataAccessLayer DAL = new DAL.DataAccessLayer();
    DAL.Open();
    SqlParameter[] param = new SqlParameter[2];

    param[0] = new SqlParameter("@ID", SqlDbType.Int);
    param[0].Value = ID;

    param[1] = new SqlParameter("@Weight", SqlDbType.Float);
    param[1].Value = DBNull.Value;
    param[1].Direction = ParameterDirection.Output;

    DAL.ExcuteCommande("Get_Weight", param);
    DAL.Close();

    double weight = 0.0;
    if(double.TryParse(param[1]?.Value?.ToString(), out weight)
    {
       return weight;
    }
    else
    {
        throw new ArgumentException("No Item found for given ID");
    }
}