Sql 异常未在消息框中显示

问题描述 投票:0回答:3

尝试插入重复项时出现 SQL 异常,但 SQL 错误未发送到消息框。我可以在调试中看到错误。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using static AccountManager.productinsert.BusinessLogic;

namespace AccountManager
{
  public partial class ProductAdd : Form
  {
    public ProductAdd()
    {
      InitializeComponent();
    }

    public void CleartextBoxes1()
    {
      ProductId.Clear();
      ProductName.Clear();            
      ProductPrice.Clear();
    }

    private void BtnClose_Click(object sender, EventArgs e)
    {
      this.Close();
    }

    private void BtnSubmit_Click(object sender, EventArgs e)
    {
      try
      {
        var product = new ProductDetails();
        product.ProductId = ProductId.Text;
        product.ProductName = ProductName.Text;
        product.ProductPrice = ProductPrice.Text;
        AddProduct(product);

        MessageBox.Show("Product Created!");
        CleartextBoxes1();
      }
      catch (SqlException)
      {
          MessageBox.Show("Product Already Exists");
      }
    }
  }
}

调试的错误是

异常 捕获异常:“System.Data.SqlClient.SqlException” AccountManager.exe(“违反唯一键约束 “UQ__产品__B40CC6CC7010A856”。无法插入重复的键 对象“dbo.Products”。重复的键值为 (1)。声明 已终止。”)System.Data.SqlClient.SqlException

按要求添加产品代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace AccountManager
{
    class productinsert
    {

        public class BusinessLogic
        {
            public static void AddProduct(ProductDetails details)
            {
                using (SqlConnection openCon = new SqlConnection("Data Source=.;Initial Catalog=AccountMGR;Integrated Security=True;"))
                {
                    var statement = "insert into Products(" +
                           "  ProductId, ProductName, Price)" +
                           " values(" +
                           "  @ProductId," +
                           "  @ProductName," +
                           "  @Price)";



                    using (SqlCommand queryInsert = new SqlCommand(statement))
                    {
                        queryInsert.Connection = openCon;

                        queryInsert.Parameters.Add("@ProductId", SqlDbType.VarChar, 30).Value = details.ProductId;
                        queryInsert.Parameters.Add("@ProductName", SqlDbType.VarChar, 30).Value = details.ProductName;
                        queryInsert.Parameters.Add("@Price", SqlDbType.VarChar, 30).Value = details.ProductPrice;



                        openCon.Open();
                        try
                        {
                            queryInsert.ExecuteNonQuery();
                        }
                        catch (SqlException)
                        {

                        }
                    }
                }


            }




            public class ProductDetails
            {

                public string ProductId { get; set; } = "";
                public string ProductName { get; set; } = "";
                public string ProductPrice { get; set; } = "";


            }
        }
    }
}
c# sql messagebox
3个回答
3
投票

试试这个

try
{
     SaveData();
}
catch (Exception ex)
{
     var sqlException = ex.InnerException as System.Data.SqlClient.SqlException;

     if (sqlException.Number == 2601)
     {
         MessageBox.show("Product Already Exists");
     }
     else
     {
         MessageBox.show(ex.Message());
     }
}

2627 表示唯一约束错误

2601 表示重复键行错误

希望这对你有帮助!


1
投票

如果你使用了try catch,你应该抛出异常来catch

try
{
    queryInsert.ExecuteNonQuery();
}
catch 
{
   throw;
}

1
投票

您应该在将军

SqlException
之前赶上
Exception
。并定义您的
SqlException
的对象实例,下面的代码将解决您的问题:

try
{
    queryInsert.ExecuteNonQuery();
}
catch (SqlException e)
{

    if (e.Number == 2601)
    {
        MessageBox.Show("Your Record is already exists");
    }
    else
    {
        MessageBox.Show(e.Message);
    }
}
catch(Exception e)
{
    // other kind of exception
}

备注:

2601 表示重复键行错误

2627 表示唯一约束错误

© www.soinside.com 2019 - 2024. All rights reserved.