【发布时间】: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