我是 c# 的新手,我有这个任务。它的一部分是让一个功能工作。我没有收到任何错误,但在运行它时也没有得到响应。你能看看我的代码并告诉我如何让返回的“钱”显示在文本框和消息框中吗?
private void button2_Click(object sender, EventArgs e)
{
SqlConnection conn = Database.GetConnection();
SqlDataReader rdr = null;
using (SqlConnection a = Database.GetConnection())
using (SqlCommand cmd = new SqlCommand("SELECT CalcRentalCharge", a))
cmd.CommandType = CommandType.StoredProcedure;
string CarRentalNo = "1";
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(
"CalcRentalCharge", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(
new SqlParameter("@RentalStartDateTime", RentalStartDateTimeBox.Text));
cmd.Parameters.Add(
new SqlParameter("@RentalEndDateTime", RentalEndDateTimeBox.Text));
cmd.Parameters.Add(
new SqlParameter("@CarTypeID", CarTypeID.Text));
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
RentalChargeBox.Text = rdr["@Money"].ToString();
MessageBox.Show("@Money");
}}
catch
{
if (conn != null)
{
conn.Close();
}
if (rdr != null)
{
rdr.Close();
}
}
}
存储过程如下所示:
USE [CarRental_P117365]
GO
/****** Object: UserDefinedFunction [dbo].[CalcRentalCharge] Script Date: 8/15/2013 09:06:09 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
/* Create Function CalcFinanceMonthlyPayment to calculate finance monthly repayment */
ALTER FUNCTION [dbo].[CalcRentalCharge] (
@CarTypeID INT,
@RentalStartDateTime DATETIME,
@RentalEndDateTime DATETIME) RETURNS MONEY
AS
BEGIN
DECLARE @NumDays INT
DECLARE @DailyRate MONEY
IF (IsNull(@CarTypeID, 0) <= 0) OR (@RentalStartDateTime IS NULL) OR (@RentalEndDateTime IS NULL) OR (@RentalEndDateTime <= @RentalStartDateTime)
RETURN 0
SELECT @DailyRate = DailyRate FROM CarType WHERE CarTypeID = @CarTypeID
IF (IsNull(@DailyRate, 0) <= 0)
RETURN 0
SELECT @NumDays = CEILING(DATEDIFF(mi, @RentalStartDateTime, @RentalEndDateTime)/ 1440.00)
RETURN CONVERT(MONEY, @NumDays * @DailyRate)
END