背景:我正在转换按邮政编码,计划类型和年龄搜索计划的访问运行时间。
到目前为止,我主要向下搜索存储过程get_zip_plan_age
正确显示但不确定我是否在C#代码隐藏中正确调用存储过程get_lowest_female_insurance_rate
以及如何为get_lowest_female_rate
编写不同的代码,显示每个年龄组的最低女性比率,显示所有数据的get_zip_plan_age
。
问题:
get_lowest_female_rate
?我的意见是否正确?get_lowest_female_rate
的存储过程代码,该代码显示每个年龄组(65,70,75,80)的最低女性费率?以下是访问运行时的屏幕截图:
这是我的default.aspx.cs代码:
protected void Search_Zip_Plan_Age_Button_Click(object sender, EventArgs e)
{
using (SqlConnection cn = new SqlConnection())
{
cn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["PriceFinderConnectionString"].ToString();
cn.Open();
using (SqlCommand cmd = cn.CreateCommand())
{
cmd.CommandText = "get_zip_plan_age";
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "get_lowest_female_rate";
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "get_lowest_male_rate";
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "get_carrier_info";
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter parm = cmd.CreateParameter();
parm.ParameterName = "@insur_age";
parm.DbType = DbType.Int64;
parm.Value = Convert.ToInt64(this.insur_age.Text);
cmd.Parameters.Add(parm);
parm = cmd.CreateParameter();
parm.ParameterName = "@zip_code";
parm.DbType = DbType.String;
parm.Value = this.ZipCode.Text;
cmd.Parameters.Add(parm);
parm = cmd.CreateParameter();
parm.ParameterName = "@plan_code";
parm.DbType = DbType.String;
parm.Value = this.PlanCode.Text;
cmd.Parameters.Add(parm);
SqlDataReader reader = cmd.ExecuteReader();
Zip_Plan_Age_GridView.DataSource = reader;
Zip_Plan_Age_GridView.DataBind();
}
}
}
以下是显示所有数据的存储过程get_zip_plan_age的代码:
ALTER PROCEDURE get_zip_plan_age
-- Add the parameters for the stored procedure here
@zip_code nvarchar(16),
@plan_code nvarchar(16),
@insur_age int = 0
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
IF @insur_age > 0
BEGIN
SELECT
[state_code],
[zip_code],
[female_value],
[male_value],
[carrier_name],
[update_date],
[insur_age],
[plan_code],
[spousal_discount]
FROM [state_zipcode_plans]
WHERE (([insur_age] = @insur_age)
AND ([zip_code] = @zip_code)
AND ([plan_code] = @plan_code))
ORDER BY [male_value], [plan_code]
END
ELSE BEGIN
SELECT
[state_code],
[zip_code],
[female_value],
[male_value],
[carrier_name],
[update_date],
[insur_age],
[plan_code],
[spousal_discount]
FROM [state_zipcode_plans]
WHERE (([zip_code] = @zip_code)
AND ([plan_code] = @plan_code))
ORDER BY [male_value], [plan_code]
END
END
GO
答案 0 :(得分:1)
在方法Search_Zip_Plan_Age_Button_Click中,您已多次设置cmd.CommandText和cmd.CommandType。在这种情况下,它只应该只设置一次 - 它们的值为cmd.CommandText =“get_carrier_info”; cmd.CommandType = CommandType.StoredProcedure;
为了使代码更易于维护,请考虑将命令的创建分离到工厂command pattern。
就sp而言 - 我只能看到sp为get_zip_plan_age。
答案 1 :(得分:1)
我的建议是:如果要将内容绑定到用户可以查看/编辑的表单,请不要使用SqlDataReader
。这将使数据库连接保持打开状态 - 可能需要很长时间!
如果您使用表单来显示数据,我会使用ORM(对象关系映射器)从数据库中获取对象列表 - 或者如果这不是您想要的,那么至少使用{{ 1}}获取数据,然后允许您与数据库断开连接,直到您实际需要更新内容(或读取新数据)。
至于调用存储过程:
DataTable
块!! 时,所以我尝试使用这样的东西:
using
答案 2 :(得分:0)
使用逻辑
这是我用于获得最低女性比率的存储过程:
ALTER PROCEDURE [dbo].[get_lowest_female_rate]
-- Add the parameters for the stored procedure here
@zip_code nvarchar(16),
@plan_code nvarchar(16),
@insur_age int = 0
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
-- If an Insurance Age is selected, only display the min Female rate for that age
IF @insur_age > 0
BEGIN
SELECT TOP(1)
[carrier_name],
[insur_age],
MIN([female_value])
FROM [state_zipcode_plans]
WHERE (([insur_age] = @insur_age)
AND ([zip_code] = @zip_code)
AND ([plan_code] = @plan_code)
AND ([female_value] > 0))
GROUP BY
[insur_age], [female_value], [carrier_name]
END
-- If ALL is selected for Insurance Age, Display Lowest Female Rate for Each of the Age Groups 65, 70, 75, 80
ELSE BEGIN
SELECT TOP(1)
[carrier_name],
[insur_age],
MIN([female_value])
FROM [state_zipcode_plans]
WHERE (([zip_code] = @zip_code)
AND ([plan_code] = @plan_code)
AND ([female_value] > 0)
AND ([insur_age] = 65))
GROUP BY
[insur_age], [female_value], [carrier_name]
UNION
SELECT TOP(1)
[carrier_name],
[insur_age],
MIN([female_value])
FROM [state_zipcode_plans]
WHERE (([zip_code] = @zip_code)
AND ([plan_code] = @plan_code)
AND ([female_value] > 0)
AND ([insur_age] = 70))
GROUP BY
[insur_age], [female_value], [carrier_name]
UNION
SELECT TOP(1)
[carrier_name],
[insur_age],
MIN([female_value])
FROM [state_zipcode_plans]
WHERE (([zip_code] = @zip_code)
AND ([plan_code] = @plan_code)
AND ([female_value] > 0)
AND ([insur_age] = 75))
GROUP BY
[insur_age], [female_value], [carrier_name]
UNION
SELECT TOP(1)
[carrier_name],
[insur_age],
MIN([female_value])
FROM [state_zipcode_plans]
WHERE (([zip_code] = @zip_code)
AND ([plan_code] = @plan_code)
AND ([female_value] > 0)
AND ([insur_age] = 80))
GROUP BY
[insur_age], [female_value], [carrier_name]
END
END