Using System;
Using System.Collections.Generic;
Using System.Configuration;
Using System.Data;
Using System.Data.Common;
Using System.Reflection;
Using System.Web;
Using Iportalactive.db;
Using MySql.Data.MySqlClient;
<summary>
@author: [Email protected] Modified
</summary>
Namespace Csutility
{
public enum Operationtype
{
Create,
Remove,
Update,
Delete
}
public class Operationfactory
{
public static Abstractdbfactory createfactory (Operationtype opttype)
{
Switch (opttype)
{
Case Operationtype.create:
return new Executefactory ();
Case Operationtype.remove:
return new Queryfactory ();
Case Operationtype.update:
return new Updatefactory ();
Case Operationtype.delete:
return new Deletefactory ();
Default:break;
}
}
}
Public abstract class Abstractdbfactory
{
}
public static class mdbmapper<t> where T:class, new ()
{
Static Mdbmapper ()
{
}
public static bool Add (T model)
{
String sql = Generateinsert (model);
Mysqlparameter[] Paras = generatemysqlparamarray (model);
int rowsaffected = Dbhelpermysql.executesql (SQL, paras);
if (rowsaffected > 0)
{
return true;
}
return false;
}
public static bool Update (T model)
{
String sql = Generateupdate (model);
Mysqlparameter[] Paras = generatemysqlparamarray (model);
int rowsaffected = Dbhelpermysql.executesql (SQL, paras);
if (rowsaffected > 0)
{
return true;
}
return false;
}
public static bool Delete (T model)
{
String sql = Generatedelete (model);
Mysqlparameter[] Paras = generatemysqlparamarray (model);
int rowsaffected = Dbhelpermysql.executesql (SQL, paras);
if (rowsaffected > 0)
{
return true;
}
return false;
}
public static ilist<t> Query (T model)
{
String sql = Generateselect (model);
Mysqlparameter[] Paras = generatemysqlparamarray (model);
DataSet ds = Dbhelpermysql.query (SQL, paras);
return reflactorhelper<t>. Dstoilist (DS);
}
public static int GetCount (T model)
{
throw new NotImplementedException ();
}
#region Update Field
private static string Generateupdate (T model)
{
if (typeof (T). GetProperty ("Id"). Equals (null) | | typeof (T). GetProperty ("id"). Equals (NULL))
{
throw new ArgumentNullException ("Id");
}
String sql = "Update" + typeof (T). Name.tolower () + "set";
SQL + = Getwherestring (model, true);
SQL + = "where id=?" Id ";
return SQL;
}
#endregion
#region Insert Field
private static string Generateinsert (T model)
{
String sql = "Insert into" + typeof (T). Name.tolower ();
string cols = getcolsstring (model, NULL);
String vals = getcolsstring (model, "?");
SQL + = cols;
SQL + = "values";
SQL + = Vals;
return SQL;
}
private static mysqlparameter[] Generatemysqlparamarray (T model)
{
Propertyinfo[] PS = model. GetType (). GetProperties ();
ilist<mysqlparameter> list = new list<mysqlparameter> ();
for (int i = 0; i < PS. Length; i++)
{
if (Ps[i]. GetValue (model, NULL) = = NULL) continue;
List. ADD (New Mysqlparameter ("?" + ps[i). Name,
Convert.changetype (Ps[i]. GetValue (model, NULL), Ps[i]. GetValue (model, NULL). GetType ())));
}
var paras = new mysqlparameter[list. Count];
for (int i = 0; I < paras. Length; i++)
{
Paras[i] = List[i];
}
return paras;
}
private static string getcolsstring (T model, string prefix)
{
Propertyinfo[] ps = typeof (T). GetProperties ();
string cols = "(";
for (int i = 0; i < PS. Length; i++)
{
if (Ps[i]. GetValue (model, NULL) = = NULL) continue;
cols + = (prefix?? "") + Ps[i]. Name + ",";
}
cols = cols. TrimEnd (', ') + ")";
return cols;
}
#endregion
#region Delete Field
public static string Generatedelete (T model)
{
String sql = "Delete from" + typeof (T). Name.tolower () + "where";
SQL + = Getwherestring (model, false);
return SQL;
}
private static string getwherestring (T model, BOOL isupdate)
{
Propertyinfo[] ps = typeof (T). GetProperties ();
string w = isupdate? "": "1=1 and";
for (int i = 0; i < PS. Length; i++)
{
if ((Isupdate && ps[i). Name.tolower (). Equals ("id")) | | Ps[i]. GetValue (model, NULL) = = NULL | |
(Ps[i]. Name.tolower (). Equals ("id") && ps[i]. GetValue (model, NULL). Equals (0))) continue;
W + = Ps[i]. Name + "=?" + ps[i]. Name + (isupdate?) ",": "and");
}
W = isupdate? W.trimend (', '): w.substring (0, w.length-4);
Return w;
}
#endregion
#region Select Field
public static string Generateselect (T model)
{
String sql = "SELECT * from" + typeof (T). Name.tolower () + "where";
SQL + = Getwherestring (model, false);
return SQL;
}
#endregion
#region SelectCount Field
#endregion
}
public static Class Mreqmapper
{
public static T getmdl<t> () where T:class, new ()
{
HttpRequest request = HttpContext.Current.Request;
var model = new T ();
var keys = new String[request. Form.AllKeys.Length + request. QueryString.AllKeys.Length];
Request. QueryString.AllKeys.CopyTo (keys, 0);
Request. Form.AllKeys.CopyTo (keys, request.) QueryString.AllKeys.Length);
Propertyinfo[] Properties = typeof (T). GetProperties ();
foreach (PropertyInfo p in properties)
{
Try
{
String val = Request. Form[p.name]?? Request. Querystring[p.name];
P.setvalue (model, Convert.changetype (Val, p.propertytype), NULL);
}
catch (Exception)
{
}
}
return model;
}
}
public class Keyattribute:attribute
{
}
public class singleton<t> where T:new ()
{
public static T Instance = new T ();
}
public class reflactorhelper<t> where T:new ()
{
public static ilist<t> Dstoilist (DataSet ds, int tbindex)
{
DataTable dt = ds. Tables[tbindex];
ilist<t> list = new list<t> ();
DataRowCollection rows = dt. Rows;
propertyinfo[] PiS = typeof (T). GetProperties ();
foreach (DataRow dr in rows)
{
T m = Drtomodel (DR);
List. ADD (m);
}
return list;
}
public static ilist<t> Dstoilist (DataSet DS)
{
Return Dstoilist (ds, 0);
}
public static T Drtomodel (DataRow Row)
{
var m = new T ();
foreach (DataColumn col in row. Table.columns)
{
foreach (PropertyInfo pi in typeof (T). GetProperties ())
{
Try
{
if (Row[pi. Name.tolower ()]! = DBNull.Value)
{
Pi. SetValue (M, Convert.changetype (Row[pi. Name.tolower ()], pi. PropertyType), NULL);
}
}
catch (Exception)
{
}
}
}
return m;
}
}
public enum DbType
{
MSSQL,
Mysql
Sqlite
}
public interface Idbhelper
{
int ExecuteSQL (String sql, params dbparameter[] paras);
DataSet Query (String sql, params dbparameter[] paras);
Object Getsingle (String sql, params dbparameter[] paras);
int GetMaxID (String sql, params dbparameter[] paras);
}
public class Connection
{
Private readonly string _db;
Public Connection (String db)
{
_db = configurationmanager.appsettings[db];
}
public string Db
{
get {return _db;}
}
}
public class Mysqlhelper:idbhelper
{
Private Connection _con;
Private Mysqlconnection _mycon;
Public Mysqlhelper (Connection con)
{
_con = con;
_mycon = new Mysqlconnection (Con. DB);
}
#region Idbhelper Members
public int ExecuteSQL (String sql, params dbparameter[] paras)
{
throw new NotImplementedException ();
}
Public DataSet Query (String sql, params dbparameter[] paras)
{
throw new NotImplementedException ();
}
public Object Getsingle (String sql, params dbparameter[] paras)
{
throw new NotImplementedException ();
}
public int GetMaxID (String sql, params dbparameter[] paras)
{
throw new NotImplementedException ();
}
#endregion
}
}
Share the Orm Class I wrote about. NET 2.0 to implement MVC. Can be used in environments such as WebForm, which is the principle part of ORM.