1
我正在使用T-SQL,這是我可以讀取的存儲過程之一,具體取決於我的參數(@para_type
)是Employee
還是Consultant
,將執行以下語句並觸發觸發器。存儲過程在SQL Server Management Studio(SSMS)中執行時正在運行。爲什麼我的C#字符串不影響我的存儲過程IF語句?
當我輸入以下內容:
execute update_person @para_salary = 800, @para_ssn = 17,
@para_companyName = 'Ikea',@para_expdate = '202',
@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的觀點:
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;
}
}
我猜**參數結束時的** CR **符號或** space \ tab **。將@para_type ='Consultant''更改爲'if @para_type LIKE'Consultant%''。對於'else',如果@para_type ='Employee'' =>'else if @para_type LIKE'Employee%'',則相同。或者不允許用戶輸入參數類型,最好使用單選按鈕。 – lad2025
@para_salary預計將是一個varchar,但您傳遞一個整數。解決此問題之前。 – Steve
@ lad2025剛剛嘗試過,但它不會工作。我將嘗試在SQL Server Management Studio中調試 –