您的位置:首页 > 数据库

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();                

            }            

        }

                   

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐