All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
全面接触SQL语法(1)_[SQL,Server教程]
Delphi变量、类、对象、函数规范化命名
ASP.NET实例:在GridView使用HyperLinkField,属性的链接_[Asp.Net教程]
PHP入门:初学来看配置文件PHP.INI的中文注释_php资料_编程技术
全面接触SQL语法(7)_mssql学习_编程技术
ASP.NET2.0中Treeview,的Checkboxes选中根枝叶全选中的实现方法_.net资料_编程技术
PHP中数组元素升序、降序及重新排序的函数_php资料_编程技术
ASP.NET&Spring.NET&NHibernate最佳实践(一)——目录_[Asp.Net教程]
通过web.config设置数据库连接串_[Asp.Net教程]
扩展GridView控件(九)——给数据行增加右键菜单_[Asp.Net教程]
Reading number is top 10 pictures
2012 national geographic daily picture9
西班牙山村小景3
Sora aoi in China3
中国女孩大胆自拍,显露完美身材3
The money of more than 100 countries and regions22
Absolutely shocked. National geographic 50 animal photographys4
More attractive than sora aoi4
西游日记4
美女和狗狗2
浴室里的美女
Download software ranking
C#高级编程(第4版)
jdk1.5
Dance with duck(male prostitution)
虚拟机汉化软件
Sora aoi‘s film--Lust fan wall
天龙八部最新服务端
WebService在.NET中的实战应用教学视频 → 第5集
圣殿祭司的ASP.NET.2.0.开发详解-使用C#
电脑知识及技巧大合集
Kung.Fu.Panda.2
delv published in(发表于) 2014/1/6 9:07:14 Edit(编辑)
dotnet下生成简单sql语句_[Asp.Net教程]

dotnet下生成简单sql语句_[Asp.Net教程]

dotnet下生成简单sql语句_[Asp.Net教程]


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


作者:liuxiaoyi666 来源:博客园





添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.