1

这是我的代码:

Document doc = new Document();

SqlConnection cnn = Ketnoi.GetSqlConnection();
cnn.Open();

SqlTransaction trans = cnn.BeginTransaction();

        doc.setChomuon(false);
        doc.setGia(txtGia.Text.ToString().Trim());
        doc.setISBN(txtISBN.Text.ToString().Trim());
        doc.setKhosach(txtKHosach.Text.ToString().Trim());
        doc.setMann(cbMangonngu.Text.ToString().Trim());
        doc.setManxb(cbManxb.Text.ToString().Trim());
        doc.setMatl(cbMatheloai.Text.ToString().Trim());
        doc.setNoidung(txtNoidung.Text.ToString());
        doc.setSlxb(txtLXB.Text.ToString().Trim());
        doc.setSotrang(txtLXB.Text.ToString().Trim());
        doc.setTensach(txtTensach.Text.ToString().Trim());
        doc.setTgxb(dtbNgayxb.Text.ToString());
        doc.setTinhtrang(true);
        doc.setMangantu(cbMangantu.Text.ToString().Trim());
        doc.setMasach(txtMasach.Text.ToString().Trim());

SqlCommand cmd = new SqlCommand();

if (rdSach.Checked == true)
{
   cmd = new SqlCommand("checkdoc", cnn, trans);
}
else if (rdSachdt.Checked == true)
{
   cmd = new SqlCommand("checkedoc", cnn, trans);
}

cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue(@"ISBN", doc.getISBN());
cmd.Parameters.AddWithValue(@"masach", doc.getMasach());
SqlParameter paramReturnValue = new SqlParameter();
paramReturnValue = cmd.Parameters.Add("RETURN_VALUE", SqlDbType.Int);
paramReturnValue.Direction = ParameterDirection.ReturnValue;

try
{
    cmd.ExecuteNonQuery();
}
catch (Exception mess)
{
    MessageBox.Show(mess.Message);
    cnn.Close();
}

if ((int)cmd.Parameters["RETURN_VALUE"].Value == 1)
{
   MessageBox.Show(" ISBN EXISTED ! ");
   cnn.Close();
}
else if ((int)cmd.Parameters["RETURN_VALUE"].Value == 0)
{
    cmd.Parameters.AddWithValue(@"TENSACH", doc.getTensach());
    cmd.Parameters.AddWithValue(@"KHOSACH", doc.getKhosach());
    cmd.Parameters.AddWithValue(@"NOIDUNG", doc.getNoidung());
    cmd.Parameters.AddWithValue(@"HINHANHPATH", doc.getHinhanh());
    cmd.Parameters.AddWithValue(@"NGAYXUATBAN", DateTime.Parse(doc.getTgxb()));
    cmd.Parameters.AddWithValue(@"LANXUATBAN", int.Parse(doc.getSlxb()));
    cmd.Parameters.AddWithValue(@"SOTRANG", int.Parse(doc.getSotrang()));
    cmd.Parameters.AddWithValue(@"GIA", Int64.Parse(doc.getGia()));
    cmd.Parameters.AddWithValue(@"MANXB", int.Parse(doc.getManxb()));
    cmd.Parameters.AddWithValue(@"MANGONNGU", int.Parse(doc.getMann()));
    cmd.Parameters.AddWithValue(@"MATL", doc.getMatl());
    cmd.Parameters.AddWithValue(@"TINHTRANG", doc.getTinhtrang());
    cmd.Parameters.AddWithValue(@"CHOMUON", doc.getChomuon());
    cmd.Parameters.AddWithValue(@"MANGANTU", int.Parse(doc.getMangantu()));

    if (rdSach.Checked == true)
        cmd.CommandText = "insert";
    else if (rdSachdt.Checked == true)
    {
        cmd.CommandText = "insertedoc";
        cmd.Parameters.AddWithValue(@"filesach", doc.getFilesach());
        cmd.Parameters.AddWithValue(@"sldown", Int64.Parse(doc.getSolandown()));
        cmd.Parameters.AddWithValue(@"down", doc.getChophepdown());
    }

    try
    {
        cmd.ExecuteNonQuery();
        MessageBox.Show("success");
        cnn.Close();
    }
    catch (Exception mess)
    {
        MessageBox.Show(mess.Message);
        trans.Rollback();
        cnn.Close();
    }
}

我在 SQL Server 中的存储过程:

ALTER proc [dbo].[checkdoc] (@ISBN varchar(10), @Masach varchar(10))
as
   IF Exists(select * from SACH where ISBN = @ISBN or MASACH=@Masach)
     return 1
   ELSE
     return 0

ALTER proc [dbo].[insertdoc](
   @ISBN char(10),
   @TENSACH varchar(100),
   @KHOSACH varchar(5),
   @NOIDUNG varchar(300),
   @HINHANHPATH varchar(50),
   @NGAYXUATBAN smalldatetime,
   @LANXUATBAN int,
   @SOTRANG int,
   @GIA bigint,
   @MANXB int,
   @MANGONNGU int,
   @MATL char(5),
   @MASACH varchar(10),
   @TINHTRANG bit,
   @CHOMUON bit,
   @MANGANTU int
)       
as
insert into ISBN values(
@ISBN,
@TENSACH,
@KHOSACH,
@NOIDUNG,
@HINHANHPATH,
@NGAYXUATBAN,
@LANXUATBAN,
@SOTRANG,
@GIA,
@MANXB,
@MANGONNGU,
@MATL 
)
insert into SACH values(@ISBN, @MASACH, @TINHTRANG, @CHOMUON, @MANGANTU)

调试时一切正常。在我收到消息成功后,检查 SQL Server,没有任何行受到影响。我确实刷新了表,我的存储过程在 SQL Server 中工作,但是当我在 asp.net 中调用它时,它不起作用。任何帮助都会很棒。

4

1 回答 1

4

我对 C# 和 Sql 集成不太熟悉,但我很确定您在最后一个 try-catch 块中缺少事务提交:

        try
        {
            cmd.ExecuteNonQuery();
            MessageBox.Show("success");
            cnn.Close();
        }
        catch (Exception mess)
        {
            MessageBox.Show(mess.Message);
            trans.Rollback();
            cnn.Close();
        }

应该是这样的:

        try
        {
            cmd.ExecuteNonQuery();
            MessageBox.Show("success");
            trans.Commit();
            cnn.Close();
        }
        catch (Exception mess)
        {
            MessageBox.Show(mess.Message);
            trans.Rollback();
            cnn.Close();
        }

在关闭连接之前注意“trans.Commit()”。

于 2013-05-20T05:48:26.113 回答