나는이 질문을 더 일찍 물었고 문제의 원인을 찾았지만 생각하지 않았습니다. boolean 매개 변수를 저장 프로 시저에 전달하는 데 문제가 있습니다. 여기 내 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
저장 프로 시저가 DropDownList로 채울 예정이다. IF 문을 실행하기로되어 있지만, IF는 건너 뛰고 대신 ELSE가 실행됩니다.
"업로드"에 "1"을 전달하여 작동하는지 보셨습니까? – PoweredByOrange
방금 코드를 업데이트했으며 C# 코드의 업로드 변수는 전역이며 초기 값은 false입니다. 그런 다음 showDate()에서 true로 할당되지만 어떻게 든 getMonthList()에서는 false로 유지됩니다. 이 코드는 getMonthList()에서 변수를 업로드하는 데 true를 할당 한 경우에만 작동합니다. 나는 이유를 모른다. – gg17
코드에 문제가 보이지 않습니다. 코드를 이해하기 위해 여러 개의 중단 점을 넣어야 할 수도 있습니다. 'getMonthList()'가 클래스의 같은 인스턴스에서 호출 되었습니까? – PoweredByOrange