【问题标题】:Check if value is in SQL Server database or not and show in message检查值是否在 SQL Server 数据库中并显示在消息中
【发布时间】:2016-12-07 21:39:11
【问题描述】:

这是插入数据的BL类:

public  string CategoryIsert(clsCategoryPL objCategory, out int returnId)
{
    returnId = 0;

    try
    {
        var db = new KSoftEntities();

        var category = new tblCategory
        {
            Name = objCategory.Name,
            ParentCategoryID = objCategory.ParentCategoryID,
            description = objCategory.description,
            image = objCategory.image,
            Status = objCategory.Status
        };

        //db.AddTotblCategories(category);
        db.tblCategories.Add(category);
        db.SaveChanges();
        returnId = category.CategoryID;
    }
    catch (Exception ex) { }

    if (returnId > 0)
         return "User Inserted Successfully";
     else
         return "Error on insertion";
}

增值的aspx代码:

private int AddCategory()
{
    clsCategoryBL objcategory = new clsCategoryBL();
    clsCategoryPL objCategoryPL = new clsCategoryPL();

    int retnid = 0;

    objCategoryPL.description = txtCategoryDescription.Text;
    objCategoryPL.Name = txtCategoryName.Text;
    objCategoryPL.ParentCategoryID = Convert.ToInt32(ddlParentCategory.SelectedValue);
    objCategoryPL.Status = true;

    objcategory.CategoryIsert(objCategoryPL, out retnid);

    if (retnid > 0)
    {
        if (Convert.ToInt32(ddlParentCategory.SelectedValue) == 0)
        {
              objCategoryPL.ParentCategoryID = retnid;
        }

        objCategoryPL.CategoryID = retnid;
        string strMessage = objcategory.CategoryUpdate(objCategoryPL);
    }

    return retnid;
 }

我在数据库中创建了一个存储过程:

CREATE PROCEDURE [dbo].[Sp_Checknm_Cat]
   @ID int,
   @NAME nvarchar(400),
   @Count INT = 0
AS
BEGIN
    DECLARE @output int

    IF(@ID > 0)
    BEGIN
        SET @Count = (select count(*) from tblCategory 
                      where Name = @NAME and CategoryID <> @ID)
    END
    ELSE
    BEGIN
        SET @Count = (select count(*) from tblCategory where Name = @NAME)
    END

    IF(@Count > 0)
    BEGIN
        SET @output = 0 
    END
    ELSE
    BEGIN
        SET @output = 1 
    END

    RETURN @output
END

我想在插入/更新期间检查该名称是否已经存在,然后它会在标签中显示一条错误消息

那么哪里需要改变呢?

这是给 bl 的:

 ClsDB objdb = new ClsDB();
 public Int32 InsertnmCheck(int id, string nm)
 {
     DataTable dtdonor = new DataTable();
     SqlParameter[] param = new SqlParameter[2];

     param[0] = new SqlParameter("@ID", SqlDbType.Int);
     param[0].Direction = ParameterDirection.Input;
     param[0].Value = id;

     param[1] = new SqlParameter("@NAME", SqlDbType.NVarChar);
     param[1].Direction = ParameterDirection.Input;
     param[1].Value = nm;

     int a = objdb.insert_delete_update("[Sp_Checknm_Cat]", param);
     return a;
 } 

这是我的后端代码:

else if (btnSubmit.CommandName == "Add")
{
   clsCategoryBL obj = new clsCategoryBL();
   Int32 dt = obj.InsertnmCheck(0, txtCategoryName.Text);
   //   DataTable dt = obj.InsertnmCheck(0, txtCategoryName.Text);

   {

   }

   int retid = AddCategory();
   if (retid > 0)
   {
   }

问题是查询(sp)返回 0 但在 bl 类中它将返回 -1 那么有什么解决办法吗??

【问题讨论】:

标签: c# asp.net sql-server


【解决方案1】:

抛出异常。

CREATE PROCEDURE [dbo].[usp_Checknm_Cat] /* do not prefix with 'sp' */
@ID int,
@NAME nvarchar(400),
@Count INT=0

AS
BEGIN
DECLARE @output int


    if(@ID>0)
    begin
        set @Count= @Count + (select count(*) from tblCategory where Name=@NAME and CategoryID<>@ID)
    end
    else
    begin
        set @Count= @Count + (select count(*) from tblCategory where Name=@NAME)
    end


    if(@Count>0)
    BEGIN;
      THROW 51000, 'Duplicates Exist', 1;
    END;


END

和c#

public void /* VOID, not a return code or return string */ CategoryIsert(clsCategoryPL objCategory)
{

    try
    {
        var db = new KSoftEntities();

        var category = new tblCategory
        {
            Name = objCategory.Name,
            ParentCategoryID = objCategory.ParentCategoryID,
            description = objCategory.description,
            image = objCategory.image,
            Status = objCategory.Status
        };

        //db.AddTotblCategories(category);
        db.tblCategories.Add(category);
        db.SaveChanges();
        returnId = category.CategoryID;
    }
    catch (SqlException sqlex) {
           /* you can examine the sql exception here, if you want to look for the 51000 */
           throw;
 }


    catch (Exception ex) {
           throw;
 }


}

【讨论】:

  • if(@Count>0) 开始; THROW 51000,“存在重复项”,1;结尾;这是什么??怎么回到这里??
  • tsql 代码使用 Sql Server 2012 为我编译。您没有标记您的 Sql Server 版本。此代码将引发异常。您的 c# 代码将捕获 SqlException。请阅读我在 cmets 中发布的文章。停止考虑返回码,考虑异常。
猜你喜欢
  • 2014-04-22
  • 2020-08-16
  • 1970-01-01
  • 1970-01-01
  • 2010-10-15
  • 2015-10-24
  • 2020-08-25
  • 1970-01-01
  • 2012-09-07
相关资源
最近更新 更多