1
私はT-SQLを使用していますが、私のパラメータ(@para_type
)がEmployee
またはConsultant
であることに応じて、これは私のストアドプロシージャの1つで、次の文が実行され、トリガが発生します。ストアドプロシージャは、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#Winフォームビュー:
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 **シンボルまたは**スペース\タブ**がパラメータの最後にあると思います。もし@para_type = 'コンサルタント'を 'if @para_type LIKE 'コンサルタント%'に変更してください。 'else if @para_type = 'Employee'' =>' else if @para_type LIKE' Employee% ''と同じです。または、ユーザーがパラメータータイプを入力することを許可しないで、ラジオボタンを使用することをお勧めします。 – lad2025
@para_salaryはvarcharであると予想されますが、整数を渡します。前にこの問題を修正してください。 – Steve
@ lad2025ちょうど試しましたが、うまく動作しません。 SQL Server Management Studioでデバッグしようとしています –