nhibernate 一个简单类用于执行sql。。问题肯定是很大,不过只是零时用下么,问题还是不大。==后面在慢慢改吧。
用户要求,可以自己输入其自己乱定义的sql。。无法,随便弄了个
估计有问题。不过也无法。先用这把
本来把想存储field类别,可木找到这么获取,先留着吧,以后该下。
Code
/// <summary>
/// 数据库对应字段
/// </summary>
public class MySQLField
{
private string _name = String.Empty;
private string _value = String.Empty;
private Type _type = null;
public string Name
{
get { return _name; }
set { _name = value; }
}
public string Value
{
get { return _value; }
set { _value = value; }
}
public Type FieldType
{
get { return _type; }
set { _type = value; }
}
/// <summary>
/// 重新保证数据tostring为对
/// </summary>
/// <returns></returns>
public override string ToString()
{
return Value;
}
/// <summary>
/// 从写hashcode ,保证list contain方法验证正确
/// </summary>
/// <returns></returns>
public override int GetHashCode()
{
return Name.ToString().GetHashCode();
}
/// <summary>
/// 数据库对应字段
/// </summary>
public class MySQLField
{
private string _name = String.Empty;
private string _value = String.Empty;
private Type _type = null;
public string Name
{
get { return _name; }
set { _name = value; }
}
public string Value
{
get { return _value; }
set { _value = value; }
}
public Type FieldType
{
get { return _type; }
set { _type = value; }
}
/// <summary>
/// 重新保证数据tostring为对
/// </summary>
/// <returns></returns>
public override string ToString()
{
return Value;
}
/// <summary>
/// 从写hashcode ,保证list contain方法验证正确
/// </summary>
/// <returns></returns>
public override int GetHashCode()
{
return Name.ToString().GetHashCode();
}
重写了下[]和add 不过因为偷懒木继承IList ,不知道会出问题不
Code
/// <summary>
/// 查询结果行
/// </summary>
public class MySQLRow : System.Collections.Generic.List<MySQLField>
{
/// <summary>
/// 提供方便的数组获取数据
/// </summary>
/// <param name="index">对于数据查询字段名</param>
/// <returns></returns>
public new MySQLField this[string index]
{
set
{
base.Add(value);
}
get
{
return this.GetField(index);
}
}
/// <summary>
/// string:key,string:value
/// </summary>
private System.Collections.Hashtable _strField = new Hashtable();
/// <summary>
/// 根据key获取值
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public string GetFieldValues(string key)
{
if (_strField.ContainsKey(key))
return (string)_strField[key];
else
return string.Empty;
}
/// <summary>
/// 根据可以获取一个字段
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public MySQLField GetField(string key)
{
if(_strField.ContainsKey(key))
return new MySQLField()
{
Name = key,
Value = GetFieldValues(key)
};
return null;
}
/// <summary>
/// 重新add方法
/// </summary>
/// <param name="item"></param>
public new void Add(MySQLField item)
{
string key = item.Name;
if (_strField.ContainsKey(key))
_strField.Remove(key);
_strField.Add(key, item.Value);
if (Contains(item))
_strField.Remove(item);
base.Add(item);
}
}
/// <summary>
/// 查询结果行
/// </summary>
public class MySQLRow : System.Collections.Generic.List<MySQLField>
{
/// <summary>
/// 提供方便的数组获取数据
/// </summary>
/// <param name="index">对于数据查询字段名</param>
/// <returns></returns>
public new MySQLField this[string index]
{
set
{
base.Add(value);
}
get
{
return this.GetField(index);
}
}
/// <summary>
/// string:key,string:value
/// </summary>
private System.Collections.Hashtable _strField = new Hashtable();
/// <summary>
/// 根据key获取值
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public string GetFieldValues(string key)
{
if (_strField.ContainsKey(key))
return (string)_strField[key];
else
return string.Empty;
}
/// <summary>
/// 根据可以获取一个字段
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public MySQLField GetField(string key)
{
if(_strField.ContainsKey(key))
return new MySQLField()
{
Name = key,
Value = GetFieldValues(key)
};
return null;
}
/// <summary>
/// 重新add方法
/// </summary>
/// <param name="item"></param>
public new void Add(MySQLField item)
{
string key = item.Name;
if (_strField.ContainsKey(key))
_strField.Remove(key);
_strField.Add(key, item.Value);
if (Contains(item))
_strField.Remove(item);
base.Add(item);
}
}
因为前台用nvelocity list[i][j] 这个用不了。只有写个中间方法用下了。
Code
/// <summary>
/// 数据查询结果
/// </summary>
public class MySQLRst : System.Collections.Generic.List<MySQLRow>
{
private int _rstNums = default(int);
public int RstNums
{
set
{
_rstNums = value;
}
get
{
return _rstNums;
}
}
/// <summary>
/// 根据行号,和key获取值
/// </summary>
/// <param name="row"></param>
/// <param name="key"></param>
/// <returns></returns>
public MySQLField GetRowField(int row ,string key)
{
if (row > Count)
return null;
return this[row][key];
}
/// <summary>
/// 根据
/// </summary>
/// <param name="row"></param>
/// <returns></returns>
public MySQLRow GetRow(int row)
{
if (row > Count)
return null;
return this[row];
}
}
/// <summary>
/// 数据查询结果
/// </summary>
public class MySQLRst : System.Collections.Generic.List<MySQLRow>
{
private int _rstNums = default(int);
public int RstNums
{
set
{
_rstNums = value;
}
get
{
return _rstNums;
}
}
/// <summary>
/// 根据行号,和key获取值
/// </summary>
/// <param name="row"></param>
/// <param name="key"></param>
/// <returns></returns>
public MySQLField GetRowField(int row ,string key)
{
if (row > Count)
return null;
return this[row][key];
}
/// <summary>
/// 根据
/// </summary>
/// <param name="row"></param>
/// <returns></returns>
public MySQLRow GetRow(int row)
{
if (row > Count)
return null;
return this[row];
}
}
方法先挂这把。hibernate的封装类,我从codeproject下弄下来的,改了下。。当然你可以用codesmith生成,
自己写也可以,都一样了。
Code
/// <summary>
/// 执行sql
/// </summary>
public class SQLHandle
{
protected NHibernateISessionManager sessionFactory = null;
public SQLHandle()
{
//获取hibernate连接
sessionFactory = NHibernateISessionManager.Instance;
}
public enum EQueryInfo
{
OK,
QUERYER
}
/// <summary>
/// 执行sql
/// </summary>
/// <param name="sqlStr"></param>
/// <returns></returns>
public EQueryInfo Query(string sqlStr)
{
return this.Query(sqlStr, CommandType.Text);
}
/// <summary>
/// 根据类型执行不同的sql
/// </summary>
/// <param name="sqlStr"></param>
/// <param name="type"></param>
/// <returns></returns>
public EQueryInfo Query(string sqlStr,CommandType type)
{
ITransaction trans = sessionFactory.GetSession().BeginTransaction();
try
{
IDbCommand cmd = sessionFactory.GetSession().Connection.CreateCommand();
cmd.CommandText = sqlStr;
cmd.CommandType = type;
trans.Enlist(cmd);//绑定存储过程
cmd.Prepare();
cmd.ExecuteNonQuery();
trans.Commit();
}
catch
{
trans.Rollback();
return EQueryInfo.QUERYER;
}
return EQueryInfo.OK;
}
/// <summary>
/// 获取数据
/// </summary>
/// <param name="sqlStr"></param>
/// <returns></returns>
public MySQLRst GetRst(string sqlStr)
{
IDbCommand cmd = sessionFactory.GetSession().Connection.CreateCommand();
cmd.CommandText = sqlStr;
var rst = new MySQLRst();
try
{
IDataReader rs = cmd.ExecuteReader();
int rstCount = 0;
while (rs.Read())
{
rstCount++;
int fieldCount = rs.FieldCount;
var row = new MySQLRow();
for (int i = 0; i < fieldCount; i++)
{
row.Add(new MySQLField()
{
Name = rs.GetName(i),
Value = rs.GetValue(i).ToString()
});
}
rst.Add(row);
}
rst.RstNums = rstCount;
}catch(Exception e)
{
throw new Exception(e.ToString());
}
return rst;
}
}
/// <summary>
/// 执行sql
/// </summary>
public class SQLHandle
{
protected NHibernateISessionManager sessionFactory = null;
public SQLHandle()
{
//获取hibernate连接
sessionFactory = NHibernateISessionManager.Instance;
}
public enum EQueryInfo
{
OK,
QUERYER
}
/// <summary>
/// 执行sql
/// </summary>
/// <param name="sqlStr"></param>
/// <returns></returns>
public EQueryInfo Query(string sqlStr)
{
return this.Query(sqlStr, CommandType.Text);
}
/// <summary>
/// 根据类型执行不同的sql
/// </summary>
/// <param name="sqlStr"></param>
/// <param name="type"></param>
/// <returns></returns>
public EQueryInfo Query(string sqlStr,CommandType type)
{
ITransaction trans = sessionFactory.GetSession().BeginTransaction();
try
{
IDbCommand cmd = sessionFactory.GetSession().Connection.CreateCommand();
cmd.CommandText = sqlStr;
cmd.CommandType = type;
trans.Enlist(cmd);//绑定存储过程
cmd.Prepare();
cmd.ExecuteNonQuery();
trans.Commit();
}
catch
{
trans.Rollback();
return EQueryInfo.QUERYER;
}
return EQueryInfo.OK;
}
/// <summary>
/// 获取数据
/// </summary>
/// <param name="sqlStr"></param>
/// <returns></returns>
public MySQLRst GetRst(string sqlStr)
{
IDbCommand cmd = sessionFactory.GetSession().Connection.CreateCommand();
cmd.CommandText = sqlStr;
var rst = new MySQLRst();
try
{
IDataReader rs = cmd.ExecuteReader();
int rstCount = 0;
while (rs.Read())
{
rstCount++;
int fieldCount = rs.FieldCount;
var row = new MySQLRow();
for (int i = 0; i < fieldCount; i++)
{
row.Add(new MySQLField()
{
Name = rs.GetName(i),
Value = rs.GetValue(i).ToString()
});
}
rst.Add(row);
}
rst.RstNums = rstCount;
}catch(Exception e)
{
throw new Exception(e.ToString());
}
return rst;
}
}
一个简单测试,好像性能木测试,估计嘿嘿。问题大大
Code
[TestFixture]
public class CommSQLTest
{
[Test]
public void select测试()
{
SQLHandle handle = new SQLHandle();
MySQLRst rst = handle.GetRst("SELECT * FROM catalog cg");
for (var i = 0; i < rst.Count;i++ )
{
System.Console.WriteLine("id= {0}--{1}--{2}", rst[i]["id"],rst[i].GetFieldValues("parent_id"),rst.GetRowField(i,"xid"));
}
}
[Test]
public void query测试()
{
SQLHandle handle = new SQLHandle();
MySQLRst rst = handle.GetRst("SELECT * FROM catalog cg where id = 1");
string sql = "update catalog set intro = 'yyy' where id = 1";
handle.Query(sql);
MySQLRst rst2 = handle.GetRst("SELECT * FROM catalog cg where id = 1");
System.Console.WriteLine("{0}-{1}",rst[0]["intro"],rst2[0]["intro"]);
}
}
[TestFixture]
public class CommSQLTest
{
[Test]
public void select测试()
{
SQLHandle handle = new SQLHandle();
MySQLRst rst = handle.GetRst("SELECT * FROM catalog cg");
for (var i = 0; i < rst.Count;i++ )
{
System.Console.WriteLine("id= {0}--{1}--{2}", rst[i]["id"],rst[i].GetFieldValues("parent_id"),rst.GetRowField(i,"xid"));
}
}
[Test]
public void query测试()
{
SQLHandle handle = new SQLHandle();
MySQLRst rst = handle.GetRst("SELECT * FROM catalog cg where id = 1");
string sql = "update catalog set intro = 'yyy' where id = 1";
handle.Query(sql);
MySQLRst rst2 = handle.GetRst("SELECT * FROM catalog cg where id = 1");
System.Console.WriteLine("{0}-{1}",rst[0]["intro"],rst2[0]["intro"]);
}
}