【问题标题】:stored procedure call to project and check for exists record c#存储过程调用项目并检查是否存在记录 c#
【发布时间】:2020-12-03 08:17:45
【问题描述】:

我有这个存储过程:

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 的类,这个调用有这个方法来添加、更新或删除:

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 的类,它具有保存客户的代码:

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();
}

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

在此先感谢 :)

【问题讨论】:

    标签: c# .net stored-procedures visual-studio-2017


    【解决方案1】:

    在按钮点击中做这样的事情

    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 
    }
    

    【讨论】:

    • 你能修改上面的代码吗?
    • 检查现在在查询中添加您的表和 id,并将您的代码添加到 if (!emp_exist) 中,并在 else 部分中添加消息,我希望它有所帮助
    【解决方案2】:

    您可以将位类型的输出参数添加到存储过程参数和 如果您的条件有任何记录,则将其设置为 1。 之后,您可以检查输出参数并决定要做什么。

    【讨论】:

    • 我无法发布代码但它不起作用:(
    【解决方案3】:

    在执行存储过程之前,您可以执行一个“选择语句”并调用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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-11-21
      • 1970-01-01
      • 2013-05-19
      • 1970-01-01
      • 2014-07-28
      • 2011-08-08
      • 2011-02-20
      相关资源
      最近更新 更多