Sql Server根据表名生成查询的存储过程(查询条件可选)
static void Main(string[] args) { string 表名 = "water_emstime"; string sql = "exec GetTableSelect " + 表名; string ConString = "server=xxx.xxx.xx.xx;database=newFW;uid=sa;pwd=sa"; SqlDataAdapter da = new SqlDataAdapter(sql, ConString); DataSet ds = new DataSet(); da.Fill(ds); string ParmsList = ""; foreach (DataRow item in ds.Tables[0].Rows) { ParmsList += item[0].ToString() + "\n"; } string Select = ""; Select += ds.Tables[1].Rows[0][0].ToString()+"\n"; foreach (DataRow item in ds.Tables[2].Rows) { Select += item[0].ToString() + "\n"; } //根据表生成的查询存储过程 string procSelect = string.Format(@"create proc [dbo].[get_{0}] ( {1} ) as begin {2} end", 表名, ParmsList, Select);//表名 、参数列表 、查询语句 // create proc [dbo].[oper_water_alarm] //( // @Param_monitoring_mn varchar(32)=Null, // @Param_factor_id varchar(32)=Null, // @Param_datatime datetime=Null, // @Param_value numeric(9)=Null, // @Param_flag varchar(10)=Null, // @Param_action int //) //as //begin // if(@Param_action=0) // begin // insert into water_alarm(monitoring_mn, //factor_id, //datatime, //value, //flag) // values (@Param_monitoring_mn, //@Param_factor_id, //@Param_datatime, //@Param_value, //@Param_flag); // end // else if(@Param_action=1) // begin // delete water_alarm // where monitoring_mn=@Param_monitoring_mn // end // else // begin // Update water_alarm set monitoring_mn=@Param_monitoring_mn, // factor_id=@Param_factor_id , // datatime=@Param_datatime , // value=@Param_value , // flag=@Param_flag // where monitoring_mn=@Param_monitoring_mn // end //end }