我之前问过这个问题,我想我发现了什么问题,但我没有。我有一个问题传递布尔参数到存储过程。这里是我的C#代码:将一个布尔参数传递给SQL Server存储过程
public bool upload = false;
protected void showDate(object sender, EventArgs e)
{
if (Radio1.Checked)
{
upload = true;
Radio2.Checked = false;
date_div.Visible = true;
date_div2.Visible = false;
}
}
protected void getMonthList()
{
selectedYear = year.SelectedValue.ToString();
SqlConnection connection = new SqlConnection(connString);
SqlCommand cmd = connection.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
connection.Open();
cmd.CommandText = "getMonth";
cmd.Parameters.Add("@year", SqlDbType.Int, 0).Value = Convert.ToInt32(selectedYear);
cmd.Parameters.AddWithValue("@upload", upload);
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
month.DataSource = dt;
month.DataTextField = "month";
month.DataValueField = "monthValue";
month.DataBind();
month.Items.Insert(0, new ListItem("Select", "0"));
}
这是存储过程getMonth
:
ALTER PROCEDURE [dbo].[getMonth]
@year int,
@upload Bit
AS
BEGIN
IF @upload = 1
BEGIN
SELECT distinct datename(mm, Upload_date) month
,month (upload_date) monthValue
FROM dbo.RESOLVED
WHERE datepart(yyyy, upload_date) = @year
ORDER by 2
END
ELSE
BEGIN
SELECT distinct datename(mm, substring(CREATE_DT,1,2) + '.' + substring(CREATE_DT,3,2) + '.' + substring(CREATE_DT,5,4)) month
,month (substring(CREATE_DT,1,2) + '.' + substring(CREATE_DT,3,2) + '.' + substring(CREATE_DT,5,4)) monthValue
FROM dbo.RESOLVED
WHERE datepart(yyyy, substring(CREATE_DT,1,2) + '.' + substring(CREATE_DT,3,2) + '.' + substring(CREATE_DT,5,4)) = @year
ORDER by 2
END
存储过程应该填充下拉列表。它应该执行IF语句,但是IF被跳过而ELSE被执行。
你有没有试过用'1'来传递'upload'来查看它是否有效? – PoweredByOrange
我刚刚更新了代码,c#代码中的上传变量是全局变量,其初始值为false。然后它在showDate()中被赋值为true,但以某种方式在getMonthList()中保持为false。该代码只在将getMonthList()中的变量赋值为true时有效。我不知道为什么。 – gg17
我没有看到任何代码错误。您可能需要在代码中添加几个断点才能找出结果。是'getMonthList()'调用类的同一个实例吗? – PoweredByOrange