|
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;
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;
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;
}
}
}
}
|
|
|