static void Main (string[] args) {string table name = "Water_emstime"; String sql = "Exec gettableselect" + table name; 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"; }//query stored procedure generated from table string procselect = string. Format (@ "CREATE proc [dbo].[ Get_{0}] ({1}) As Begin {2} end ", table name, Parmslist, select );//table name, parameter list, query statement//CREATE PROC [dbo]. [Oper_water_alarm]//(//@Param_monitoring_mn varchar (+) =null, @Param_factor_id varchar (=null),//@Param_datatime datetime=null,// @Param_value Numeric (9) =null,//@Param_flag varchar (=null),//@Param_ac tion int//)//as//begin//if (@Param_acti on=0)//Begin//INSERT INTO Water_alarm (MONITORING_MN, factor_id,//datatime,//value, Flag)//VALUES (@Param_monitoring_mn, @Param_factor_id,//@Param_datatime,//@Para M_value,//@Param_flag); End/Else if (@Param_action =1)//Begin/DE Lete Water_alarm//WHERE [EMAIL PROTECTED]_MONITORING_MN//End else/Begin//Update water_alarm Set [email p ROTECTED]_MONITORING_MN,//[email protected]_factor_id,//[EMA Il protected]_datatime,//[Email protected]_value,//[EM Ail protected]_flag//WHERE [EMAIL PROTECTED]_MONITORING_MN// End//end}
SQL Server stored procedure for query generation based on table name (query criteria optional)