Beispiel #1
0
        int DeleteModel(IMODEL obj)
        {
            int executeNum = 0;

            if (obj != null)
            {
                Type                 type     = obj.GetType();
                PropertyInfo[]       p        = type.GetProperties();
                List <KeyBox>        kbox     = GetKeyList(p, obj);
                List <DataParameter> listPara = new List <DataParameter>();
                string               whereStr = "";
                try
                {
                    int js = 0;
                    foreach (KeyBox box in kbox)
                    {
                        whereStr += (js == 0 ? "" : " AND ") + box.Key + "=" + box.Dbpara.ParameterName;
                        listPara.Add(new DataParameter(box.Dbpara.ParameterName, box.Dbpara.Value));
                        js++;
                    }
                }
                catch (Exception exp)
                {
                    throw exp;
                }
                executeNum = ExecuteNonQuery("DELETE FROM  " + GetTableName(obj.GetType()) + " WHERE " + whereStr, listPara);
            }
            return(executeNum);
        }
Beispiel #2
0
        int AddModel(IMODEL obj)
        {
            int executeNum = 0;

            if (obj != null)
            {
                List <DataParameter> listPara = new List <DataParameter>();
                Type           type           = obj.GetType();
                string         info           = "";
                string         infoValue      = "";
                PropertyInfo[] p = type.GetProperties();
                int            i = 0;
                try
                {
                    foreach (PropertyInfo propInfo in p)
                    {
                        if (propInfo.GetValue(obj, null) != null)
                        {
                            if (i > 0)
                            {
                                info      += ",[" + propInfo.Name + "]";
                                infoValue += ",@" + propInfo.Name + "" + "";
                            }
                            else
                            {
                                info      += "[" + propInfo.Name + "]";
                                infoValue += "@" + propInfo.Name + "" + "";
                            }
                            string paravalue = propInfo.GetValue(obj, null) + "";
                            if (propInfo.PropertyType.FullName.ToLower().IndexOf("datetime") != -1)
                            {
                                paravalue = DateTime.Parse(paravalue).ToString("yyyy-MM-dd");
                            }
                            listPara.Add(new DataParameter("@" + propInfo.Name, paravalue));
                            i++;
                        }
                    }
                }
                catch (Exception exp)
                {
                    throw exp;
                }
                executeNum = ExecuteNonQuery("INSERT INTO  " + GetTableName(obj.GetType()) + " (" + info + ") VALUES(" + infoValue + ")", listPara);
            }
            return(executeNum);
        }
Beispiel #3
0
 public void CopyMyToModel(IMODEL obj, List <string> notKey)
 {
     PropertyInfo[] p = obj.GetType().GetProperties();
     foreach (PropertyInfo toinfo in p)
     {
         foreach (PropertyInfo pinfo in this.GetType().GetProperties())
         {
             if (toinfo.Name == pinfo.Name && notKey.IndexOf(pinfo.Name) == -1)
             {
                 if (pinfo.GetValue(this, null) != null)
                 {
                     toinfo.SetValue(obj, pinfo.GetValue(this, null), null);
                 }
                 break;
             }
         }
     }
 }
Beispiel #4
0
 int AddModel(IMODEL obj)
 {
     obj = (IMODEL)dbContext.Set(obj.GetType()).Add(obj);
     return(1);
 }
Beispiel #5
0
        int UpdateModel(IMODEL obj)
        {
            int executeNum = 0;

            if (obj != null)
            {
                Type                 type     = obj.GetType();
                string               info     = "";
                PropertyInfo[]       p        = type.GetProperties();
                List <DataParameter> listPara = new List <DataParameter>();
                List <KeyBox>        kbox     = GetKeyList(p, obj);
                foreach (KeyBox item in kbox)
                {
                    if (string.IsNullOrEmpty(item.Dbpara.Value + ""))
                    {
                        throw new Exception("未将主键赋值");
                    }
                }
                int i = 0;
                try
                {
                    foreach (PropertyInfo propInfo in p)
                    {
                        //object[] list = propInfo.GetCustomAttributes(true).Where(infos => infos.GetType().Name == "ModelAttribute").ToArray();
                        List <object> list = new List <object>();
                        foreach (var item in propInfo.GetCustomAttributes(true))
                        {
                            if (item.GetType().Name == "ModelAttribute")
                            {
                                list.Add(item);
                            }
                        }
                        if (list.Count != 0)
                        {
                            ModelAttribute matt = list[0] as ModelAttribute;
                            if (propInfo.GetValue(obj, null) != null && !matt.ModelContent.IsKey)
                            {
                                info += (i != 0 ? "," : "") + "[" + propInfo.Name + "]=@" + propInfo.Name;
                                DbParameter par = dbComm.CreateParameter();
                                par.ParameterName = "@" + propInfo.Name;
                                string paravalue = propInfo.GetValue(obj, null) + "";
                                if (propInfo.PropertyType.FullName.ToLower().IndexOf("datetime") != -1)
                                {
                                    paravalue = DateTime.Parse(paravalue).ToString("yyyy-MM-dd");
                                }
                                listPara.Add(new DataParameter("@" + propInfo.Name, paravalue));
                                i++;
                            }
                        }
                    }
                }
                catch (Exception exp)
                {
                    throw exp;
                }
                string UpdateStr = "UPDATE " + GetTableName(obj.GetType()) + " SET " + info;

                if (kbox.Count != 0)
                {
                    int js = 0;
                    foreach (KeyBox box in kbox)
                    {
                        UpdateStr += (js == 0 ? " WHERE " : " AND ") + box.Key + "=@" + box.Key;
                        listPara.Add(new DataParameter(box.Dbpara.ParameterName, box.Dbpara.Value));
                        js++;
                    }
                    executeNum = ExecuteNonQuery(UpdateStr, listPara);
                }
            }
            return(executeNum);
        }