sqlhelper(根据自身使用情况不断完善中)
2018-01-31 15:28
232 查看
public enum DB
{
SqlServer
}
public class SQLHelper
{
static object lkobj = new object();
public IDbConnection connection;
public IDbCommand cmd;
public DB db;
public SQLHelper(DB db, string conStr)
{
switch (db)
{
case DB.SqlServer:
connection = new System.Data.SqlClient.SqlConnection(conStr);
break;
}
this.db = db;
cmd = connection.CreateCommand();
}
public int ExecuteNonQuery(string sql)
{
try
{
cmd.CommandText = sql;
connection.Open();
return cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
connection.Close();
}
}
public object ExecuteScalar(string sql)
{
try
{
cmd.CommandText = sql;
connection.Open();
return cmd.ExecuteScalar();
}
catch (Exception ex)
{
throw ex;
}
finally
{
connection.Close();
}
}
public List<T> ExecuteReader<T>(string sql) where T : new()
{
try
{
List<T> list = new List<T>();
Type type = typeof(T);
PropertyInfo[] ps = type.GetProperties();
cmd.CommandText = sql;
connection.Open();
IDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
T t = new T();
list.Add(t);
foreach (PropertyInfo pInfo in ps)
{
pInfo.SetValue(t, reader[pInfo.Name] is DBNull ? null : reader[pInfo.Name]);
}
}
return list;
}
catch (Exception ex)
{
throw ex;
}
finally { connection.Close(); }
}
public int ExecuteInsert<T>(List<T> listt) where T : new()
{
lock (lkobj)
{
if (listt == null || listt.Count == 0)
{
return -1;
}
Type type = typeof(T);
PropertyInfo[] ps = type.GetProperties();
string tbName = type.Name;
string sql = "";
for (int i = 0; i < listt.Count; i++)
{
string col = "";
string val = "";
foreach (PropertyInfo p in ps)
{
object o = p.GetValue(listt[i]);
if (o != null)
{
if (!string.IsNullOrWhiteSpace(col))
{
col += ",";
}
col += p.Name;
if (!string.IsNullOrWhiteSpace(val))
{
val += ",";
}
val += "@" + p.Name + i.ToString(); ;
switch (db)
{
case DB.SqlServer:
(cmd as SqlCommand).Parameters.AddWithValue("@" + p.Name + i.ToString(), o);
break;
}
}
}
if (string.IsNullOrWhiteSpace(col) || string.IsNullOrWhiteSpace(val))
{
continue;
}
sql += string.Format("INSERT INTO {0}({1})VALUES({2});", tbName, col, val);
}
cmd.CommandText = sql;
try
{
connection.Open();
int res = cmd.ExecuteNonQuery();
return res;
}
catch (Exception ex)
{
throw ex;
}
finally
{
cmd.Parameters.Clear();
connection.Close();
}
}
}
public int ExecuteInsert<T>(T t) where T:new()
{
Type type = typeof(T);
PropertyInfo[] ps = type.GetProperties();
string tbName = type.Name;
string col = "";
string val = "";
foreach (PropertyInfo p in ps)
{
object o = p.GetValue(t);
if (o != null)
{
if (!string.IsNullOrWhiteSpace(col))
{
col += ",";
}
col += p.Name;
i
4000
f (!string.IsNullOrWhiteSpace(val))
{
val += ",";
}
val += "@" + p.Name;
switch (db)
{
case DB.SqlServer:
(cmd as SqlCommand).Parameters.AddWithValue("@" + p.Name, o);
break;
}
}
}
if (string.IsNullOrWhiteSpace(col) || string.IsNullOrWhiteSpace(val))
{
return -1;
}
string sql = string.Format("INSERT INTO {0}({1})VALUES({2})", tbName, col, val);
cmd.CommandText = sql;
try
{
connection.Open();
int res = cmd.ExecuteNonQuery();
return res;
}
catch (Exception ex)
{
throw ex;
}
finally
{
cmd.Parameters.Clear();
connection.Close();
}
}
}
{
SqlServer
}
public class SQLHelper
{
static object lkobj = new object();
public IDbConnection connection;
public IDbCommand cmd;
public DB db;
public SQLHelper(DB db, string conStr)
{
switch (db)
{
case DB.SqlServer:
connection = new System.Data.SqlClient.SqlConnection(conStr);
break;
}
this.db = db;
cmd = connection.CreateCommand();
}
public int ExecuteNonQuery(string sql)
{
try
{
cmd.CommandText = sql;
connection.Open();
return cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
connection.Close();
}
}
public object ExecuteScalar(string sql)
{
try
{
cmd.CommandText = sql;
connection.Open();
return cmd.ExecuteScalar();
}
catch (Exception ex)
{
throw ex;
}
finally
{
connection.Close();
}
}
public List<T> ExecuteReader<T>(string sql) where T : new()
{
try
{
List<T> list = new List<T>();
Type type = typeof(T);
PropertyInfo[] ps = type.GetProperties();
cmd.CommandText = sql;
connection.Open();
IDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
T t = new T();
list.Add(t);
foreach (PropertyInfo pInfo in ps)
{
pInfo.SetValue(t, reader[pInfo.Name] is DBNull ? null : reader[pInfo.Name]);
}
}
return list;
}
catch (Exception ex)
{
throw ex;
}
finally { connection.Close(); }
}
public int ExecuteInsert<T>(List<T> listt) where T : new()
{
lock (lkobj)
{
if (listt == null || listt.Count == 0)
{
return -1;
}
Type type = typeof(T);
PropertyInfo[] ps = type.GetProperties();
string tbName = type.Name;
string sql = "";
for (int i = 0; i < listt.Count; i++)
{
string col = "";
string val = "";
foreach (PropertyInfo p in ps)
{
object o = p.GetValue(listt[i]);
if (o != null)
{
if (!string.IsNullOrWhiteSpace(col))
{
col += ",";
}
col += p.Name;
if (!string.IsNullOrWhiteSpace(val))
{
val += ",";
}
val += "@" + p.Name + i.ToString(); ;
switch (db)
{
case DB.SqlServer:
(cmd as SqlCommand).Parameters.AddWithValue("@" + p.Name + i.ToString(), o);
break;
}
}
}
if (string.IsNullOrWhiteSpace(col) || string.IsNullOrWhiteSpace(val))
{
continue;
}
sql += string.Format("INSERT INTO {0}({1})VALUES({2});", tbName, col, val);
}
cmd.CommandText = sql;
try
{
connection.Open();
int res = cmd.ExecuteNonQuery();
return res;
}
catch (Exception ex)
{
throw ex;
}
finally
{
cmd.Parameters.Clear();
connection.Close();
}
}
}
public int ExecuteInsert<T>(T t) where T:new()
{
Type type = typeof(T);
PropertyInfo[] ps = type.GetProperties();
string tbName = type.Name;
string col = "";
string val = "";
foreach (PropertyInfo p in ps)
{
object o = p.GetValue(t);
if (o != null)
{
if (!string.IsNullOrWhiteSpace(col))
{
col += ",";
}
col += p.Name;
i
4000
f (!string.IsNullOrWhiteSpace(val))
{
val += ",";
}
val += "@" + p.Name;
switch (db)
{
case DB.SqlServer:
(cmd as SqlCommand).Parameters.AddWithValue("@" + p.Name, o);
break;
}
}
}
if (string.IsNullOrWhiteSpace(col) || string.IsNullOrWhiteSpace(val))
{
return -1;
}
string sql = string.Format("INSERT INTO {0}({1})VALUES({2})", tbName, col, val);
cmd.CommandText = sql;
try
{
connection.Open();
int res = cmd.ExecuteNonQuery();
return res;
}
catch (Exception ex)
{
throw ex;
}
finally
{
cmd.Parameters.Clear();
connection.Close();
}
}
}
相关文章推荐
- Eclipse重要快捷键汇总(不是一次性补全,根据日常使用情况逐步完善)
- SQL/PLSQL性能优化思路和工具使用【不断完善】
- C/C++ 标准以及各种编译器对标准的支持情况(不断完善中)
- 我自己常用的linux命令及使用技巧(不断完善中)
- 使用swift 中的注意,不断完善中
- 跟大家推荐一个我们自己开发的任务管理系统,我们自己已使用多年,并且在不断完善
- windows查询端口使用情况以及根据PID查询进程
- 使用js过程中遇到的小问题总结(随使用情况不断添加)
- 一个根据内存使用情况重启tomcat的小脚本
- Android使用ViewPager实现导航页根据情况禁止滑动以及点击切换
- 基于docker的caffe环境搭建与使用示例(根据自己的情况稍作修改)
- NGUI FontMaker 简单使用,以及在文字源文件丢失的情况下,怎么根据UIFont预制获取全部文字
- windows 重装系统后必装的软件(根据个人使用情况整理,欢迎读者在评论推荐其他好用的软件)
- 技术转载(鼠标点击X窗口关闭IE的同时清空session,最基本的就是处理用户重复登陆需要用到,我想这个的关键在于如何捕捉到关闭IE这个动作,之后再根据自身的需要使用session.invalidate()或者session.removeAttribute( "xxx "))
- java读写文件的几种方式(不断完善),方便日后使用
- windows 重装系统后必装的软件(根据个人使用情况整理,欢迎读者在评论推荐其他好用的软件)
- java读写文件的几种方式(不断完善),方便日后使用
- windows 重装系统后必装的软件(根据个人使用情况整理,欢迎读者在评论推荐其他好用的软件)
- windows 重装系统后必装的软件(根据个人使用情况整理,欢迎读者在评论推荐其他好用的软件)
- linux 根据 进程名 获取 对应 PID 来 判断 使用 内存情况