Esempio n. 1
0
        public int UpdateObj2Table(object obj, string strTableName, string strWhere)
        {
            string sql = "select * from " + strTableName + " where 1!=1";
            DataSet ds = this.ExecuteDataset(sql);
            Type myType = obj.GetType();
            System.Reflection.PropertyInfo[] pInfos = myType.GetProperties();
            StringBuilder strKey = new StringBuilder();
            int count = 0;
            for (int i = 0; i < pInfos.Length; i++)
            {
                System.Reflection.PropertyInfo pPropertyInfo = pInfos[i];
                if (ds.Tables[0].Columns.Contains(pPropertyInfo.Name))
                {
                    count++;
                }
            }

            IDataParameter[] parameterValues = new IDataParameter[count];
            count = 0;
            for (int i = 0; i < pInfos.Length; i++)
            {
                System.Reflection.PropertyInfo pPropertyInfo = pInfos[i];
                if (ds.Tables[0].Columns.Contains(pPropertyInfo.Name))
                {
                    strKey.Append("," + pPropertyInfo.Name + "=:x" + pPropertyInfo.Name);
                    parameterValues.SetValue(this.GetParameter(":x" + pPropertyInfo.Name, pPropertyInfo.GetValue(obj, null)), count);
                    count++;
                }
            }

            string strSql = "update " + strTableName + " set " + strKey.ToString().Remove(0, 1) + " where " + strWhere;

            return this.ExecuteNonQuery(strSql, parameterValues);
        }
Esempio n. 2
0
        public int InsertObj2Table(object obj, string strTableName)
        {
            string sql = "select * from " + strTableName + " where 1!=1";
            DataSet ds = this.ExecuteDataset(sql);

            Type myType = obj.GetType();
            System.Reflection.PropertyInfo[] pInfos = myType.GetProperties();
            StringBuilder strKey = new StringBuilder();
            StringBuilder strValue = new StringBuilder();
            int count = 0;
            for (int i = 0; i < pInfos.Length; i++)
            {
                System.Reflection.PropertyInfo pPropertyInfo = pInfos[i];
                if (ds.Tables[0].Columns.Contains(pPropertyInfo.Name))
                {
                    count++;
                }
            }

            IDataParameter[] parameterValues = new IDataParameter[count];
            count = 0;
            for (int i = 0; i < pInfos.Length; i++)
            {
                System.Reflection.PropertyInfo pPropertyInfo = pInfos[i];
                if (ds.Tables[0].Columns.Contains(pPropertyInfo.Name))
                {
                    strKey.Append("," + pPropertyInfo.Name);
                    strValue.Append(",:x" + pPropertyInfo.Name);
                    string typename = pPropertyInfo.PropertyType.FullName;
                    if (typename == "System.Byte[]")
                    {
                        byte[] bytes = pPropertyInfo.GetValue(obj, null) as byte[];
                        if (bytes == null || bytes.Length == 0)
                        {
                            parameterValues.SetValue(this.GetParameter(":x" + pPropertyInfo.Name, DBNull.Value), count);
                        }
                        else
                        {
                            parameterValues.SetValue(this.GetParameter(":x" + pPropertyInfo.Name, pPropertyInfo.GetValue(obj, null)), count);
                        }
                    }
                    else
                    {
                        parameterValues.SetValue(this.GetParameter(":x" + pPropertyInfo.Name, pPropertyInfo.GetValue(obj, null)), count);
                    }
                    count++;
                }
            }

            string strSql = "insert into " + strTableName + " (" + strKey.ToString().Remove(0, 1) + ") values(" + strValue.ToString().Remove(0, 1) + ")";
            return this.ExecuteNonQuery(strSql, parameterValues);
        }