简体   繁体   English

如何在C#中检查数据库中是否已经存在数据

[英]How to check if data already exists in database in c#

private int AddRecordCategory()
{
        AdminAllCategory aAdminAllCategory = new AdminAllCategory();
        AdminAllCategoryBLL allCategoryBLL = new AdminAllCategoryBLL();

        int Action = 0;

        try
        {
            if (CategoryID > 0)
            {
                aAdminAllCategory.CategoryId = Convert.ToInt32(CategoryIdtxt.Text);
            }
            else
            {
                //CategoryGateway objCategoryGateway = new CategoryGateway();
                string columnName = "CategoryID";
                string tableName = "Deal.Category";
                aAdminAllCategory.CategoryId = allCategoryBLL.MaxId(columnName, tableName) + 1;
            }

            aAdminAllCategory.Category = BanglaCategory.Text;
            aAdminAllCategory.CategoryEng = EnglishCategory.Text;
            aAdminAllCategory.IsActive = Convert.ToInt32(IsActive.Text);
            aAdminAllCategory.IsHit = Convert.ToInt32(HitDropDown.Text);
            aAdminAllCategory.OrderBy = Convert.ToInt32(OrderBy.Text);

            if (Hit.Text != "")
            {
                aAdminAllCategory.hit = Convert.ToInt32(Hit.Text);
            }
            else
            {
                aAdminAllCategory.hit = 0;
            }

            aAdminAllCategory.MetaKeyword = metaKeyWord.InnerText;
            aAdminAllCategory.MetaDescription = MetaDescription.InnerText;
            aAdminAllCategory.VisibleInDevice = Convert.ToInt32(IsVisible.SelectedValue);
            aAdminAllCategory.AppColorCode = AppColorCode.Text;

            aAdminAllCategory.CategoryTitleBng = BngPageTitle.Text;
            aAdminAllCategory.CategoryTitleEng = EngPageTitle.Text;

            Action = allCategoryBLL.InsertCategory(aAdminAllCategory);
        }
        catch (Exception ex)
        {
            lblSystemMessage.Text = "Warning!!Contact With IT";
        }

        return Action;
}

Here I want to save these attributes in database but want to see if CategoryTitleEng and CategoryTitleBng properties are saved in database before these column are saved. 在这里,我想将这些属性保存在数据库中,但要查看在保存这些列之前,是否将CategoryTitleEngCategoryTitleBng属性保存在数据库中。 Then that will show a message like: 然后,将显示一条消息,如:

these property are already exists. 这些属性已经存在。

The gateway and stored procedure are shown below ..... 网关和存储过程如下所示.....

public int AddRecord_Category(AdminAllCategory aAdminAllCategory)
{
    int intActionResult = 0;
    int isActive = 1;
    //int isDeleted = 0;

    try
    {
        OpenConnection();
        ArrayList arlSqlParameters = new ArrayList();
        arlSqlParameters.Add(new SqlParameter("@CategoryID", aAdminAllCategory.CategoryId));
        arlSqlParameters.Add(new SqlParameter("@Category", aAdminAllCategory.Category));
        arlSqlParameters.Add(new SqlParameter("@IsHit", aAdminAllCategory.IsHit));
        arlSqlParameters.Add(new SqlParameter("@IsActive", aAdminAllCategory.IsActive));
        arlSqlParameters.Add(new SqlParameter("@OrderBy", aAdminAllCategory.OrderBy));
        arlSqlParameters.Add(new SqlParameter("@CategoryEng", aAdminAllCategory.CategoryEng));
        arlSqlParameters.Add(new SqlParameter("@MetaKeyword", aAdminAllCategory.MetaKeyword));
        arlSqlParameters.Add(new SqlParameter("@MetaDescription", aAdminAllCategory.MetaDescription));
        arlSqlParameters.Add(new SqlParameter("@VisibleInDevice", aAdminAllCategory.VisibleInDevice));
        arlSqlParameters.Add(new SqlParameter("@AppColorCode", aAdminAllCategory.AppColorCode));
        arlSqlParameters.Add(new SqlParameter("@CategoryTitleEng",aAdminAllCategory.CategoryTitleEng));
        arlSqlParameters.Add(new SqlParameter("@CategoryTitleBng", aAdminAllCategory.CategoryTitleBng));

            intActionResult = this.ExecuteActionQuery("Deal.USP_AddDealsCategory", arlSqlParameters);

    }
    catch (Exception ex)
    {
    }
    finally
    {
        CloseConnection();
    }

    return intActionResult;
}

CREATE PROCEDURE [Deal].[USP_AddDealsCategory]
(
    @CategoryID int,
    @Category nvarchar(150),
    @IsActive int,
    @IsHit int,
    @OrderBy int,
    @CategoryEng varchar(150),
    @MetaKeyword varchar(500),
    @MetaDescription varchar(500),
    @VisibleInDevice int = 0,
    @AppColorCode varchar(50) = '',
    @CategoryTitleEng varchar(200),
    @CategoryTitleBng nvarchar(200),
    @MetaKeywordBng nvarchar(200),
    @MetaDescriptionBng nvarchar(200)
) 
AS
BEGIN       
Insert into Deal.Category
(CategoryID,Category,IsActive,IsHit,OrderBy,Hit,CategoryEng,MetaKeyword,MetaDescription,VisibleInDevice,AppColorCode,CategoryTitleEng,CategoryTitleBng,MetaKeywordBng,MetaDescriptionBng)
Values(@CategoryID,@Category,@IsActive,@IsHit,@OrderBy,0,@CategoryEng,@MetaKeyword,@MetaDescription,@VisibleInDevice,@AppColorCode,@CategoryTitleEng,@CategoryTitleBng,@MetaKeywordBng,@MetaDescriptionBng)
END 

You have added the property @CategoryTitleBng twice to your parameter list. 您已将属性@CategoryTitleBng两次添加到参数列表中。 thats why it is giving the error This should only be once 多数民众赞成在为什么它给错误这应该只是一次

arlSqlParameters.Add(new SqlParameter("@CategoryTitleBng", aAdminAllCategory.CategoryTitleBng));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM