To generate a simple SQL statement under dotnet

Source: Internet
Author: User
Tags foreach dotnet httpcontext insert join return table name tostring
sql| statement

Static public void Insert_sql (string tablename,hashtable param_employeefield,string connstring)
  {
  //system.web.httpcontext.current.response.write ("xxx");
   Hashtable ht_field=new Hashtable ();
   Ht_field=getfieldtype (tablename,connstring);//table fields
   string field_value;
   string field_type;//field type
   string str_sql_fieldname= insert INTO + tablename ("; Insert statement
   string str_sql_fieldvalue= "values (";
   string Str_sql;
   foreach (Object Obj_param in Param_employeefield)
   {
    field_type =ht_field[obj_param. ToString ()]. ToString ();//get int or varchar type, and so on
    Field_value=param_employeefield[obj_param]. ToString ();
    Str_sql_fieldname+=param_employeefield[obj_param]. ToString () + ",";
    Str_sql_fieldvalue+=judgetype (field_type,field_value) + ",";
  }

   Str_sql_fieldname=str_sql_fieldname. Substring (1,str_sql_fieldname. Length) + ")";
   Str_sql_fieldvalue=str_sql_fieldvalue. Substring (1,str_sql_fieldvalue. Length) + ")";
   Str_sql=str_sql_fieldname+str_sql_fieldvalue;
   nsn.core.SqlHelper.ExecuteNonQuery (connstring,commandtype.text,str_sql);
 }

  static public void Update_sql (string tablename,hashtable param_employeefield,string connstring)
  {
   Hashtable ht_field=new Hashtable ();
   Ht_field=getfieldtype (tablename,connstring);
   string Field_value;
   string Field_type;
   StringBuilder str_sql  = new StringBuilder ();  
   str_sql. Append ("Update" + "TableName set");
   string sql1;
   foreach (Object Obj_param in Param_employeefield)
   {
    field_type =ht_field[obj_param. ToString ()]. ToString ();
    Field_value=param_employeefield[obj_param]. ToString ();
    Str_sql. Append (Param_employeefield[obj_param]. ToString () + "=" +judgetype (field_type,field_value) + ",");
  }
   Sql1=str_sql. ToString (). Substring (1,str_sql. ToString (). LENGTH-1) + "where";
   Nsn.core.SqlHelper.ExecuteNonQuery (ConnstriNG,COMMANDTYPE.TEXT,SQL1);
 }

  Static protected string Judgetype (String field_type,string field_value)
  {
   string Str_value;
   switch (field_type)
   {
    case "int":     str _value=field_value;
     break;
    case "varchar": str_value= "" "+field_value+" ";
     break;
    case "ntext":   str_value= "" "+field_value+" ";
     break;
    Case "datetime": str_value= "'" +field_value+ "";
     break;
    case "tinyint": str_value=field_value;
     break;
    case "smallint": str_value=field_value;
     break;
  }
   return (Field_type);
 }

  Static protected Hashtable GetFieldType (String tablename,string connstring)
  {
   DataSet ds = new DataSet ();
   Hashtable ht_field=new Hashtable ();
   sqlparameter[] paramsToStore = new Sqlparameter[1];
   paramstostore[0] = new SqlParameter ("@tablename", SqlDbType.NVarChar);
   paramstostore[0]. Direction=parameterdirection.input;
   paramstostore[0]. Value=tablename;
   
   ds=nsn.core.sqlhelper.executedataset (connstring, CommandType.StoredProcedure, "main_searchtable", paramsToStore);
   DataTable Tbl=ds. Tables[0];
   foreach (DataRow row in tbl.) Rows)
   {
   
    Ht_field. Add (row["field name"]. ToString (), row["type"]. ToString ());
   //system.web.httpcontext.current.response.write (row[field name). ToString ());
  }
   return (Ht_field);
 }

The main_searchtable stored procedure is
CREATE PROCEDURE main_searchtable
@tablename nvarchar (50)
As

SELECT
Table name =case when a.colorder=1 then D.name else "end,
Table Description =case When a.colorder=1 then IsNull (F.value, ' ") Else ' end,
Field Ordinal =a.colorder,
Field name =a.name,
Identify =case when ColumnProperty (A.id,a.name, ' isidentity ') =1 then ' √ ' Else ' end,
Type =b.name


From Syscolumns A
Left join Systypes B on A.xtype=b.xusertype
INNER JOIN sysobjects D on a.id=d.id and d.xtype= ' U ' and d.name<> ' dtproperties '

Left join Sysproperties F on d.id=f.id and f.smallid=0

where d.name= @tablename --If only the specified table is queried, plus this condition
ORDER BY A.id,a.colorder
Go







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.