我想从存储过程中返回一个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 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();
}
}
我想在业务层中使用可以返回值的方法创建一个类 - 例如
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();
}
}
请帮帮我
答案 0 :(得分:1)
如果这是代码审查,我会对您的数据访问层方法遇到一些问题,但为了解决您的问题,我建议您更改Get_Weight
方法以返回双重而不是一个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");
}
}
答案 1 :(得分:-1)
使用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;
}