1. 程式人生 > >.net 儲存過程刪除

.net 儲存過程刪除

 

public bool DeleteDelivery(string TableName, string ID)
{
SqlConnection conn = new SqlConnection(connStr);
try
{
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
SqlParameter[] ps =
{
new SqlParameter("@TableName",TableName),
new SqlParameter("@ID",ID)
};
SqlCommand cmd = new SqlCommand("proc_Delete", conn);
cmd.CommandType = CommandType.StoredProcedure;
if (ps != null)
{
foreach (SqlParameter item in ps)
{
cmd.Parameters.Add(item);
}
}
cmd.ExecuteNonQuery();
return true;
}
catch (Exception)
{
throw;
}
finally
{
if (conn.State != ConnectionState.Closed)
{
conn.Close();
}
}

}

 

 

資料庫儲存過程刪除

create procedure proc_Delete
@TableName varchar(50),
@ID varchar(5000)
as
begin
declare @strSql varchar(5000)
declare @sql varchar(4000)
set @sql='select col='''+ replace(@ID,',',''' union all select ''')+''''

if OBJECT_ID('tempdb..#DelID') is not null
drop table #DelID
create table #DelID(ID VARCHAR(36))

insert into #DelID exec(@sql)

set @strSql = 'delete from '

[email protected]+' where ID in ( select ID from #DelID)'

exec(@strSql)
end

 

 

拿過來直接用就好了  主要就是在控制器執行  需要將Table表名改成自己的表 才能進行刪除