论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > Asp.net教程
Tag:静态页面,treeview,gridview,repeater,dataset,sqldatareader,ado.net,上传,三层,ajax,xml,留言本,新闻发布,商城,注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,触发器,socket,form认证,登录,视频教程

dotnet下生成简单sql语句

文章类别:Asp.net | 发表日期:2008-10-5 22:10:40

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); //表的字段
   string field_value;
   string field_type;//字段类型
   string str_sql_fieldname="insert into " + "tablename("; //插入语句
   string str_sql_fieldvalue=" values(";
   string str_sql;
   foreach(object obj_param in param_employeefield)
   {
    field_type=ht_field[obj_param.ToString()].ToString();//获取 int型 or varchar型等等
    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["字段名"].ToString(),row["类型"].ToString());
    //System.Web.HttpContext.Current.Response.Write(row["字段名"].ToString());
   }
   return(ht_field);
  }

main_searchtable 存储过程是
CREATE PROCEDURE main_searchtable
@tablename nvarchar(50)
AS 
 
SELECT  
 表名=case when a.colorder=1 then d.name else '' end, 
 表说明=case when a.colorder=1 then isnull(f.value,'') else '' end, 
 字段序号=a.colorder, 
 字段名=a.name, 
 标识=case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end, 
 类型=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    --如果只查询指定表,加上此条件 
order by a.id,a.colorder
GO

http://liuxiaoyi666.cnblogs.com/archive/2006/05/28/411082.html

视频教程列表
文章教程搜索
 
Asp.net推荐教程
Asp.net热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058