我使用的是T-SQL,这是我可以阅读的存储过程之一,具体取决于我的参数(@para_type
)是Employee
还是Consultant
,以下语句将执行,触发器将被触发。存储过程在SQL Server Management Studio(SSMS)中执行时正在运行。
我输入以下内容:
execute update_person @para_salary = 800, @para_ssn = 17,
@para_companyName = 'Ikea',@para_expdate = '20201230',
@para_type = 'Consultant', @para_officeId = 2
但是在我的C#Winforms代码中,我使用文本框来填充T-SQL(@para_type
)的参数。它不会影响我的存储过程的IF
语句。我已经在C#中对它进行了调试,所有内容都相应地执行了,但是我的存储过程不会执行编程。
存储过程:
create procedure update_person
@para_salary varchar(25),
@para_ssn int,
@para_companyName varchar(25),
@para_expdate date,
@para_type varchar(10),
@para_officeId int
as
begin
if @para_ssn is null
print('Please enter a value for (ssn)')
else if @para_type = 'Employee'
delete from consultant
where ssn = @para_ssn
update person
set flag = @para_type
where ssn = @para_ssn
update employee
set salary = @para_salary
where ssn = @para_ssn
if @para_type = 'Consultant'
delete from employee
where ssn = @para_ssn
update person
set flag = @para_type
where ssn = @para_ssn
update consultant
set companyName = @para_companyName, expdate = @para_expdate
where ssn = @para_ssn
update works_at
set officeId = @para_officeId
where ssn = @para_ssn
end
C#Winform the view:
public void initializeComboBoxes()
{
var arr1 = new string[] { "Employee", "Consultant" };
foreach (var item in arr1)
add_person_combobox.Items.Add(item);
add_person_combobox.SelectedIndex = 0;
foreach (var item in arr1)
update_person_combobox.Items.Add(item);
update_person_combobox.SelectedIndex = 0;
}
private void lblTimer(int i)
{
var t = new Timer();
t.Interval = i;
t.Tick += (s, e) =>
{
ResponseLbl.Text = "";
t.Stop();
};
t.Start();
}
private void update_person_btn_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(update_person_salary.Text))
{
update_person_salary.Text = "0";
}
if (string.IsNullOrWhiteSpace(update_person_CoName.Text))
{
update_person_CoName.Text = "Lund Consulting AB";
}
int ssn = Convert.ToInt32(update_person_ssn.Text);
string type = update_person_combobox.SelectedItem.ToString();
int salary = Convert.ToInt32(update_person_salary.Text);
string comName = update_person_CoName.Text;
int officeId = update_person_officeId.SelectedIndex + 1;
DateTime expdate = update_person_expDate.Value.Date;
if (ctrl.update_person(salary, ssn, comName, expdate, type, officeId) == true)
{
ResponseLbl.ForeColor = System.Drawing.Color.Green;
ResponseLbl.Text = "Succesfull";
lblTimer(3000);
}
else
{
ResponseLbl.ForeColor = System.Drawing.Color.Red;
ResponseLbl.Text = "Failed";
lblTimer(3000);
}
}
C#数据访问层
public bool update_person(int ssn, int salary, string companyName, DateTime expdate, string type, int officeId)
{
try
{
con.Open();
cmd = new SqlCommand("update_person", con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@para_ssn", ssn);
cmd.Parameters.AddWithValue("@para_salary", salary);
cmd.Parameters.AddWithValue("@para_companyName", companyName);
cmd.Parameters.AddWithValue("@para_expdate", expdate);
cmd.Parameters.AddWithValue("@para_type", type);
cmd.Parameters.AddWithValue("@para_officeId", officeId);
cmd.ExecuteNonQuery();
con.Close();
return true;
}
catch (SqlException e)
{
Console.WriteLine("error: " + e.Message);
con.Close();
return false;
}
}
答案 0 :(得分:0)
你在程序块中缺少BEGIN和END ..
BEGIN
if @para_ssn is null
print('Please enter a value for (ssn)')
else if @para_type = 'Employee'
BEGIN
delete from consultant where ssn = @para_ssn
update person set flag = @para_type where ssn = @para_ssn
update employee set salary = @para_salary where ssn = @para_ssn
END
else if @para_type = 'Consultant'
BEGIN
delete from employee where ssn = @para_ssn
update person set flag = @para_type where ssn = @para_ssn
update consultant set companyName = @para_companyName, expdate = @para_expdate where ssn = @para_ssn
update works_at set officeId = @para_officeId where ssn = @para_ssn
END
END