【数据库】通用的存储过程
通用删除表存储过程:
create PROCEDURE Sp_deletedatabyCondition @tablename nvarchar(100), @condition nvarchar(200) AS BEGIN DECLARE @Sql nvarchar(500) SET @Sql='delete from '+@tablename+ ' where '+@condition EXEC(@Sql) END GO
通用更新存储过程:
create PROCEDURE Sp_UpdateTablebyCondition @tablename nvarchar(100), @condition nvarchar(300), @columns nvarchar(500) AS BEGIN DECLARE @sql nvarchar(1000) SET @sql='update '+@tablename+' set '+@columns+' where ' +@condition EXEC(@sql) END GO
通用查询表信息存储过程:
CREATE PROCEDURE Sp_getDataByTableName -- Add the parameters for the stored procedure here @tablename nvarchar(100) AS BEGIN DECLARE @sql nvarchar(500) SET @sql='select * from '+@tablename EXEC(@sql) END GO
通用查询表某列信息存储过程:
CREATE PROCEDURE SP_getColumnsbyTable @tablename nvarchar(100), @columns nvarchar(500) AS BEGIN DECLARE @sql nvarchar(1000) SET @sql='select '+@columns+' from '+ @tablename EXEC(@sql) END GO
通用查询表某列含条件存储过程:
CREATE PROCEDURE SP_getColumnsByCondition @tablename nvarchar(100), @columns nvarchar(300), @condition nvarchar(200) AS BEGIN DECLARE @sql nvarchar(1000) SET @sql='select '+@columns+' from '+@tablename+ ' where 1=1 '+@condition EXEC(@sql) END GO
demo:
public string tongyongtext(model.Client model) { using (SqlConnection con = getcon.getconns()) { using (SqlCommand com = con.CreateCommand()) { con.Open(); com.CommandText = "SP_getColumnsByCondition"; com.CommandType = CommandType.StoredProcedure; com.Parameters.Add("@tablename", SqlDbType.NVarChar); com.Parameters["@tablename"].Value = "Client"; com.Parameters.Add("@columns", SqlDbType.NVarChar); com.Parameters["@columns"].Value = "C_TwoPwd"; com.Parameters.Add("@condition", SqlDbType.NVarChar); com.Parameters["@condition"].Value = "and C_ID=" + model.cid; SqlDataAdapter da = new SqlDataAdapter(com); DataTable dt = new DataTable(); da.Fill(dt); DataRow dr = dt.Rows[0]; string twopwd = dr[0].ToString(); return twopwd; } } }