SqlConnection con = new SqlConnection(GlobalData.GetConnectionString());
string queryDepartment = null;
if (rbtnYes.Checked == true)
{
if (rbtnMale.Checked == true)
{
queryDepartment =
@"BEGIN TRY
BEGIN TRAN
insert into UserDetails
values('" + Convert.ToInt32(txtID.Text) + "','" + txtFullName.Text + "','" + txtPassword.Text + "','" + txtUserName.Text + "','" + cbDepartment.SelectedValue.ToString() + "','" + txtContactAddress.Text + "','" + Convert.ToInt64(txtContactNumber.Text.ToString()) + "','" + txtContactEmail.Text + "',CAST(GETDATE() AS DATE),'" + rbtnYes.Text + "',null,'" + rbtnMale.Text + "','" + Convert.ToInt64(txtSalary.Text.ToString()) + @"');
insert into Users
values('" + GlobalData.UsersID_AddUsers + "','" + GlobalData.RoleID_AddUsers + "','" + txtUserName.Text + "','" + Convert.ToInt32(txtID.Text) + @"');
COMMIT TRAN
END TRY
BEGIN CATCH
SELECT ERROR_NUMBER() AS ErrorNumber,ERROR_SEVERITY() AS ErrorSeverity,ERROR_STATE() AS ErrorState,ERROR_PROCEDURE() AS ErrorProcedure,ERROR_LINE() AS ErrorLine,ERROR_MESSAGE() AS ErrorMessage
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION
END CATCH";
}
}
我正在尝试使用 sql 事务将记录从 c# 代码插入到两个表中,但它不工作。
虽然我使用 sql 管理软件在 sql 表中插入相同的语句,但它的工作原理。