您好,登錄后才能下訂單哦!
本篇接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 { /// <summary> /// SQL構建器(還沒與賦值) /// </summary> public sealed class SQL_Structure { private SQL_Structure() { } /// <summary> /// 添加一個數據的T-SQL的構成 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="model"></param> /// <returns></returns> public static string ADD_T_SQL<T>( 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)); } /// <summary> /// 刪除一條數據的T-SQL構成(以ID號來刪除) /// </summary> /// <typeparam name="T"></typeparam> /// <param name="model"></param> /// <returns></returns> public static string DELETE_T_SQL<T>(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 { /// <summary> /// SqlParameter /// </summary> public sealed class SqlParameter_Structure { private SqlParameter_Structure() { } /// <summary> /// 構建T-SQL參數 ADD /// </summary> /// <typeparam name="T"></typeparam> /// <param name="model"></param> /// <returns></returns> public static SqlParameter[] ADD_T_SQL<T>(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; } /// <summary> /// 構建T-SQL參數 DELETE /// </summary> /// <typeparam name="T"></typeparam> /// <param name="model"></param> /// <returns></returns> public static SqlParameter[] DELETE_T_SQL<T>(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<User>(kayer_new);
Console.WriteLine(into_sql);
SqlParameter[] a = SqlParameter_Structure.ADD_T_SQL<User>(kayer_new);
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<User>(kayer_delete);
Console.WriteLine(delete_sql);
SqlParameter[] dedete_p = SqlParameter_Structure.DELETE_T_SQL<User>(kayer_delete);
int i = SqlHelper.ExecteNonQuery(CommandType.Text, delete_sql, dedete_p);
if (i > 0)
{
Console.WriteLine("執行操作成功");
}
else
{
Console.WriteLine("執行操作失敗");
}
#endregion
Console.Read();
}
}
}
結果如下:
數據庫
未完待續。。。。。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。