繁体   English   中英

存储过程调用项目并检查是否存在记录 c#

[英]stored procedure call to project and check for exists record c#

我有这个存储过程:

Create procedure [dbo].[Save_Customer]

    @CustomerID int output,
    @CustomerName nvarchar (150)

    

as
    if (select count(*) FROM dbo.CustomersTbl WHERE CustomersTbl.CustomerID= @CustomerID)>0
    BEGIN

        return -3
    END 
ELSE    
   if (select count(*) FROM dbo.CustomersTbl WHERE CustomersTbl.CustomerName= @CustomerName)=0
     BEGIN
       insert into CustomersTbl
                (CustomerID,CustomerName)
                values
                (@CustomerID,@CustomerName)
                return -2
     END
            else
            begin
            return -4
            
    END

在项目中,我有一个名为 DataAccessLayer 的 class 这个调用有这个方法来添加、更新或删除:

public void ExecuteCommand(string stored_procedure, SqlParameter[] param)
{
    SqlCommand sqlcmd = new SqlCommand();
    sqlcmd.CommandType = CommandType.StoredProcedure;
    sqlcmd.CommandText = stored_procedure;
    sqlcmd.Connection = sqlconnection;
    if (param != null)
    {
        sqlcmd.Parameters.AddRange(param);
    }
    sqlcmd.ExecuteNonQuery();
}

现在我创建了另一个名为业务层文件夹的层,其中包含名为 CLS_AddUpdateDelete 的 class,它具有用于保存客户的代码:

public void Save_Customers(int Customer_Id, string Customer_Name)
{
    Data_Layer.DataAccessLayer DAL = new Data_Layer.DataAccessLayer();
    DAL.Open();
    SqlParameter[] param = new SqlParameter[2];
    param[0] = new SqlParameter("@CustomerID", SqlDbType.Int);
    param[0].Value = Customer_Id;
    param[1] = new SqlParameter("@CustomerName", SqlDbType.NVarChar,150);
    param[1].Value = Customer_Name;
    DAL.ExecuteCommand("Save_Customer", param);
    DAL.Close();
}  

在我有按钮的形式中:

private void button3_Click(object sender, EventArgs e)
{
    if (textBox3.Text.Trim() == "")
    {
        MessageBox.Show("Please Enter Customer Name");
        return;
    }
    Saving.Save_Customers(Convert.ToInt32(textBox4.Text.Trim()),textBox3.Text.Trim());
    MessageBox.Show("Added Successfully", "Add Customer", MessageBoxButtons.OK, MessageBoxIcon.Information);
    ClearControls.ResetAllControls(this);
    dataGridView2.DataSource = GetInfo.GetAllFroDataGridView();
    DataTable tbl = GetLastRecord.GetLastRecord();
    textBox4.Text = (Convert.ToInt32(tbl.Rows[0][0])).ToString();
}

我想要的是如何知道这条记录是否存在于数据库中,如果它存在我需要弹出消息以使用该记录存在于数据库中。

提前致谢:)

在单击按钮时做这样的事情

Boolean emp_exist = false;
string query = select * from CustomersTbl where CustomerID = '"+yourid+"';
SqlCommand myCommand0 = new SqlCommand(query, con);
SqlDataReader myreader = myCommand0.ExecuteReader();
while (myreader.Read())
{   
 emp_exist = true; 
}
if (!emp_exist)
{
   //your code  
}  
else
{
 //add your message employee exist 
}

你可以在存储过程参数中添加一个output位类型的参数,如果有符合你条件的记录就设置为1。 之后,您可以检查 output 参数并决定要做什么。

在执行存储过程之前,您可以执行“选择语句”并调用SqlCommand.ExecuteScalar Method来检查该项目是否存在于数据库中。 如果不存在,则该方法的返回值为“null”。

你可以参考下面的代码。

string connString = @"connection string";
string sql = "select * from CustomersTbl where CustomerID='" + textBoxID.Text + "'";
using (SqlConnection conn = new SqlConnection(connString))
{
    SqlCommand cmd = new SqlCommand(sql, conn);
    try
    {
        conn.Open();
        if(cmd.ExecuteScalar() == null)
            Console.WriteLine("not exist");
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

暂无
暂无

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

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