c#一步一步实现ORM(二)

时间:2021-01-17 20:49:41

c#一步一步实现ORM(二)

 

  上一篇描述了简单的思路,这一片我们来稍微细化一下

1插入的时候忽略某些字段

       public int Insert<T>(T o, params string[] except)
        {

//有时候我们添加的时候只想添加部分字段,或者有的字段是自增的方式,那么要去掉这个字段(在sqlserver里面是这样,在mysql里面 是插入 null)

            try
            {

                Type type = typeof(T);
                var tablename = "dbo.[" + typeof(T).Name + "]";


                string fields = "";
                string values = "";
                List<SqlParameter> paras = new List<SqlParameter>();


                object v = null;
                foreach (var item in type.GetProperties())
                {



                    bool flag = true;



                    if (except != null && except.Contains(item.Name))
                    {

                        flag = false;
                    }


                    if (flag)
                    {
                        v = item.GetValue(o, null);
                        if (v != null)
                        {
                            fields += "," + "[" + item.Name + "]";
                            values += ",@" + item.Name;
                            paras.Add(new SqlParameter("@" + item.Name, v));
                        }
                    }
                }
                string sql = string.Format("insert into  {0} ({1}) values({2})", tablename, fields.Substring(1), values.Substring(1));

                return ExcuteSql(sql, paras);
            }
            catch (Exception e)
            {
                Console.Write(e.ToString());
                return 0;
            }

        }

2批量插入

       public int InsertArray<T>(IEnumerable<object> o, params string[] except)
        {
            if (o.Count() == 0)
            {

                return 0;
            }


            Type type = typeof(T);
            var tablename = "dbo.[" + typeof(T).Name + "]";
            List<SqlParameter> paras = new List<SqlParameter>();


            string sql = "";

            int count = 0;
            foreach (var i in o)
            {
                count++;
                string fields = "";
                string values = "";



                //object v = null;
                foreach (var item in type.GetProperties())
                {

                    bool flag = false;

                 
                    if (!flag)
                    {
                        var vitem = item.GetValue(i, null);

                        if (vitem != null)
                        {
                            fields += ",[" + item.Name + "]";
                            values += ",@" + count + item.Name;

                            paras.Add(new SqlParameter("@" + count + item.Name, vitem));
                        }
                    }

                }


                sql += string.Format("insert into  {0} ({1}) values({2});", tablename, fields.Substring(1), values.Substring(1));
            }



            return ExcuteSql(sql, paras);



        }

  

   private int ExcuteSql(string sql, IEnumerable<SqlParameter> paras)
        {
            using (SqlConnection conn = new SqlConnection(this.sqlConnStr))
            {

                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;

                cmd.CommandText = sql;

                cmd.Parameters.AddRange(paras.ToArray());

                conn.Open();
                int flag = 0;
                try
                {


                    flag = cmd.ExecuteNonQuery();


                }
                catch (Exception e)
                {
                    throw e;
                   
                }
                conn.Close();
                return flag;
            }
        }

  下一篇我们开始学习修改,删除,和解析lambda表达式