回滚并通知sql事务中是否出了问题

时间:2019-03-17 13:13:31

标签: sql-server winforms

具有一个代码,该代码通过SqlTransaction在Windows窗体中执行了事务,并向表中加载了数据库。如果有错误,则仅在MessageBox.Show(ex.Message);中输出。但是,现在有必要以过程的形式在数据库的服务器端处理数据。但是,如何在交易到应用程序的过程中通知用户数据是否不正确?

ALTER PROCEDURE [dbo].[addPacient]
    @name NVARCHAR(20),
    @surname NVARCHAR(20),
    @middle NVARCHAR(20),
    @passport NCHAR(10),
    @address NVARCHAR(20),
    @age INT,
    @name_diagnoz NVARCHAR(30),
    @stage CHAR(1)
AS
BEGIN TRAN  
    BEGIN TRY
        INSERT INTO Pacient(Name, Surname, Middle_name, Column__Passport,
                            Legal_address_Clinic, Age, id_diagnoz)
        VALUES (@name, @surname, @middle, @passport, 
                @address, @age, 
                (SELECT id_diagnoz FROM Diagnoz 
                 WHERE Name_diagnoz = @name_diagnoz and Stage = @stage))
    END  TRY
    BEGIN CATCH
        ROLLBACK TRAN

        RETURN
    END CATCH   
COMMIT TRAN

它的名称:

using (SqlConnection connection = new SqlConnection(connectionString))
{
    SqlCommand command = new SqlCommand("addPacient", connection);
    command.CommandType = CommandType.StoredProcedure;

    command.Parameters.AddWithValue("@name", txtName.Text);
    command.Parameters.AddWithValue("@surname",  txtSurname.Text);
    command.Parameters.AddWithValue("@middle",  txtMiddle.Text);
    command.Parameters.AddWithValue("@passport",  mskPassport.Text);
    command.Parameters.AddWithValue("@address",  cbAddres.Text);
    command.Parameters.AddWithValue("@age",  cbAge.Text);
    command.Parameters.AddWithValue("@name_diagnoz", cbxNameDiagnoz.Text);
    command.Parameters.AddWithValue("@stage", cbxStage.Text);

    connection.Open();

    using (SqlDataReader dr = command.ExecuteReader())
    {
        while (dr.Read())
        {
            int intColumn = dr.GetInt32(dr.GetOrdinal("intColumnName"));
            string stringColumn = dr.GetString(dr.GetOrdinal("stringColumnName"));
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您当前的代码中无需进行显式交易;默认情况下,单个语句自动提交事务将提供全无,并引发错误。在这种情况下,T-SQL TRY / CATCH不提供价值,因此您可以简单地使用:

ALTER PROCEDURE [dbo].[addPacient]
    @name NVARCHAR(20),
    @surname NVARCHAR(20),
    @middle NVARCHAR(20),
    @passport NCHAR(10),
    @address NVARCHAR(20),
    @age INT,
    @name_diagnoz NVARCHAR(30),
    @stage CHAR(1)
AS
INSERT INTO Pacient(Name, Surname, Middle_name, Column__Passport,
    Legal_address_Clinic, Age, id_diagnoz)
VALUES (@name, @surname, @middle, @passport, 
    @address, @age, 
        (SELECT id_diagnoz FROM Diagnoz 
    WHERE Name_diagnoz = @name_diagnoz and Stage = @stage));
GO

如果您有多个语句在显式T-SQL事务中执行数据修改,请确保在proc代码中包含SET XACT_ABORT ON,以确保在客户端超时的情况下自动回滚事务(其中CATCH块将不会执行)。我建议在多语句过程中使用此错误处理模式:

ALTER PROCEDURE [dbo].[addPacient]
    @name NVARCHAR(20),
    @surname NVARCHAR(20),
    @middle NVARCHAR(20),
    @passport NCHAR(10),
    @address NVARCHAR(20),
    @age INT,
    @name_diagnoz NVARCHAR(30),
    @stage CHAR(1)
AS
SET XACT_ABORT ON;
BEGIN TRY
    BEGIN TRAN;
    INSERT INTO Pacient(Name, Surname, Middle_name, Column__Passport,
        Legal_address_Clinic, Age, id_diagnoz)
    VALUES (@name, @surname, @middle, @passport, 
        @address, @age, 
            (SELECT id_diagnoz FROM Diagnoz 
        WHERE Name_diagnoz = @name_diagnoz and Stage = @stage));
    --other data modification statements
    COMMIT;
END TRY
BEGIN CATCH
    IF @@TRANCOUNT > 0 ROLLBACK;
    THROW;
END CATCH;
GO

此外,最好避免使用AddWithValue并传递强类型参数。这将改善服务器端和provides other benefits too上的缓存重用。

try
{
    using (SqlConnection connection = new SqlConnection(connectionString))
    using (SqlCommand command = new SqlCommand("addPacient", connection))
    {
        command.CommandType = CommandType.StoredProcedure;

        command.Parameters.Add("@name", SqlDbType.NVarChar, 20).Value = txtName.Text;
        command.Parameters.Add("@surname", SqlDbType.NVarChar, 20).Value = txtSurname.Text;
        command.Parameters.Add("@middle", SqlDbType.NVarChar, 20).Value  = txtMiddle.Text;
        command.Parameters.Add("@passport", SqlDbType.NChar, 10).Value = mskPassport.Text;
        command.Parameters.Add("@address", SqlDbType.NVarChar, 20).Value = cbAddres.Text;
        command.Parameters.Add("@age", SqlDbType.Int, 20).Value = int.Parse(cbAge.Text);
        command.Parameters.Add("@name_diagnoz", SqlDbType.NVarChar, 30).Value = cbxNameDiagnoz.Text;
        command.Parameters.Add("@stage", SqlDbType.Char, 1).Value = cbxStage.Text;

        connection.Open();

        using (SqlDataReader dr = command.ExecuteReader())
        {
            while (dr.Read())
            {
                int intColumn = dr.GetInt32(dr.GetOrdinal("intColumnName"));
                string stringColumn = dr.GetString(dr.GetOrdinal("stringColumnName"));
            }
        }
    }
}
catch (Exception ex)
{
    MessageGox.Show(ex.Message);
}