符合中小企业对网站设计、功能常规化式的企业展示型网站建设
本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...
商城网站建设因基本功能的需求不同费用上面也有很大的差别...
手机微信网站开发、微信官网、微信商城网站...
本篇接C# SQL(二)
专业领域包括成都网站制作、网站设计、购物商城网站建设、微信营销、系统平台开发, 与其他网站设计及系统开发公司不同,成都创新互联的整合解决方案结合了帮做网络品牌建设经验和互联网整合营销的理念,并将策略和执行紧密结合,为客户提供全网互联网整合方案。
在其代码上继续扩展 删操作, 让我们开始吧。。。。
一 : 构建T-SQL语句(在SQL_Structure.cs中添加一个方法 DELETE_T_SQL, 用于构建delete的T-SQL)
using DBDome.model; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; namespace DBDome.com { ////// SQL构建器(还没与赋值) /// public sealed class SQL_Structure { private SQL_Structure() { } ////// 添加一个数据的T-SQL的构成 /// ////// /// public static string ADD_T_SQL ( T model ) where T : BaseModel { string base_add = @"insert into [{0}] ({1}) values ({2})";//表名 , 列明 , 对应的值 Type model_type = model.GetType(); string[] arr = model_type.Name.Split(new char[] { '.' }); string table_name = arr[arr.Length - 1];//获得表的名称 PropertyInfo[] p_intos = model_type.GetProperties(); StringBuilder table_field_name = new StringBuilder(); StringBuilder table_field_value = new StringBuilder(); PropertyInfo item = null; for (int i = 0; i < p_intos.Length; i++) { item = p_intos[i]; if (item.Name != "id")//不应添加主键 { table_field_name.Append(item.Name + ","); table_field_value.Append("@" + item.Name + ","); } } return String.Format(base_add, table_name, table_field_name.ToString().Substring(0, table_field_name.ToString().Length-1), table_field_value.ToString().Substring(0,table_field_value.ToString().Length-1)); } /// /// 删除一条数据的T-SQL构成(以ID号来删除) /// ////// /// public static string DELETE_T_SQL (T model) where T : BaseModel { string base_delete = @"delete from [{0}] where {1} = {2}"; Type model_type = model.GetType(); string[] arr = model_type.Name.Split(new char[] { '.' }); string table_name = arr[arr.Length - 1];//获得表的名称 PropertyInfo[] p_intos = model_type.GetProperties(); PropertyInfo item = null; for (int i = 0; i < p_intos.Length; i++) { item = p_intos[i]; if (item.Name == "id") { //返回T-SQL构建 return string.Format( base_delete , table_name, item.Name, "@" + item.Name ); } } return string.Empty; } } }
二 : 构建SqlParameter参数 (在SqlParameter_Structure.as中添加方法DELETE_T_SQL)
using DBDome.model; using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Reflection; using System.Text; using System.Data; namespace DBDome.com { ////// SqlParameter /// public sealed class SqlParameter_Structure { private SqlParameter_Structure() { } ////// 构建T-SQL参数 ADD /// ////// /// public static SqlParameter[] ADD_T_SQL (T model) where T : BaseModel { Type model_type = model.GetType(); PropertyInfo[] p_intos = model_type.GetProperties(); SqlParameter[] sql_param = new SqlParameter[p_intos.Length - 1];//不需要主键 PropertyInfo item = null; string p_name = ""; SqlParameter cell = null; int j = 0; for (int i = 0; i < p_intos.Length; i++) { item = p_intos[i]; p_name = item.Name; if (p_name == "id") continue; // Console.WriteLine("字段 {0} 的类型为 {1} ", p_name ,item.PropertyType); cell = new SqlParameter(p_name, SqlParameter_Structure.Get_SqlDbType_SqlType(item.PropertyType)); cell.Value = item.GetValue(model, null); sql_param[j] = cell; j++; } return sql_param; } /// /// 构建T-SQL参数 DELETE /// ////// /// public static SqlParameter[] DELETE_T_SQL (T model) where T : BaseModel { Type model_type = model.GetType(); PropertyInfo[] p_intos = model_type.GetProperties(); SqlParameter[] sql_param = new SqlParameter[1];//只需要主键 PropertyInfo item = null; for (int i = 0; i < p_intos.Length; i++) { item = p_intos[i]; if (item.Name != "id") continue; SqlParameter cell = new SqlParameter(item.Name, SqlParameter_Structure.Get_SqlDbType_SqlType(item.PropertyType)); cell.Value = item.GetValue(model, null); sql_param[0] = cell; break; } return sql_param; } private static SqlDbType Get_SqlDbType_SqlType(Type cshaper) { string[] arr = cshaper.ToString().Split(new char[] { '.' }); string toLow = arr[arr.Length - 1].ToLower(); switch (toLow) { case "string": return SqlDbType.NVarChar; break; case "int16": return SqlDbType.Bit; break; case "int32": return SqlDbType.Int; break; default: throw new Exception(String.Format("T-SQL参数没有配对的类型 {0}" ,cshaper)); break; } } } }
三 : 测试
先查下数据库 , 看看能删除的数据:
编写测试代码 , 开始测试
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBDome.com;
using DBDome.model;
using System.Data.SqlClient;
using System.Data;
namespace DBDome
{
class Program
{
static void Main(string[] args)
{
#region 添加一条记录
/*
User kayer_new = new User();
kayer_new.name = "kayer";
kayer_new.sex = 1;
kayer_new.lv = 1;
kayer_new.username = "Kayer";
kayer_new.userpwd = "123";
string into_sql = SQL_Structure.ADD_T_SQL
Console.WriteLine(into_sql);
SqlParameter[] a = SqlParameter_Structure.ADD_T_SQL
string c = SqlHelper.connectionString;
Console.WriteLine(c);
int i = SqlHelper.ExecteNonQuery(CommandType.Text, into_sql, a);
if (i > 0)
{
Console.WriteLine("执行操作成功");
}
else
{
Console.WriteLine("执行操作失败");
}
*/
#endregion
#region 删除一条记录
User kayer_delete = new User();
kayer_delete.id = 2;
string delete_sql = SQL_Structure.DELETE_T_SQL
Console.WriteLine(delete_sql);
SqlParameter[] dedete_p = SqlParameter_Structure.DELETE_T_SQL
int i = SqlHelper.ExecteNonQuery(CommandType.Text, delete_sql, dedete_p);
if (i > 0)
{
Console.WriteLine("执行操作成功");
}
else
{
Console.WriteLine("执行操作失败");
}
#endregion
Console.Read();
}
}
}
结果如下:
数据库
未完待续。。。。。