コード例 #1
0
        /// <summary>
        ///  更新一个对象
        /// </summary>
        /// <param name="TDataBase"></param>
        public void UpdateOnlyObject(IDataBase TDataBase)
        {
            DataObjectAttribute TDataObject = TDataBase.GetType().GetCustomAttributes(typeof(DataObjectAttribute), false)[0] as DataObjectAttribute;

            string[] KeyS   = TDataObject.KeyS.Split(new char[] { ',' });
            Array    TArray = Array.CreateInstance(typeof(string), KeyS.Length);

            KeyS.CopyTo(TArray, 0);
            string TableName = TDataObject.Table;

            System.Reflection.FieldInfo[]        mFieldInfo    = TDataBase.GetType().GetFields();
            System.Data.SqlClient.SqlParameter[] TSqlParameter = new SqlParameter[mFieldInfo.Length];

            string SelSql         = "Update " + TableName + " Set ";
            string FieldValueName = string.Empty;
            string WhereName      = string.Empty;

            for (byte index = 0; index < mFieldInfo.Length; index++)
            {
                if (mFieldInfo[index].GetCustomAttributes(typeof(FieldObjectAttribute), false).Length != 0)
                {
                    FieldObjectAttribute TFieldObject = mFieldInfo[index].GetCustomAttributes(typeof(FieldObjectAttribute), false)[0] as FieldObjectAttribute;
                    //if (TFieldObject.Type == 1)
                    //{
                    TSqlParameter[index] = new SqlParameter("@" + mFieldInfo[index].Name, TDataBase.GetType().GetField(mFieldInfo[index].Name).GetValue(TDataBase));
                    //}
                }
                else
                {
                    if (Array.IndexOf(TArray, mFieldInfo[index].Name) == -1)
                    {
                        FieldValueName += FieldValueName.Length > 0 ? ", " + mFieldInfo[index].Name + "=@" + mFieldInfo[index].Name : mFieldInfo[index].Name + "=@" + mFieldInfo[index].Name;
                    }
                    TSqlParameter[index] = new SqlParameter("@" + mFieldInfo[index].Name, TDataBase.GetType().GetField(mFieldInfo[index].Name).GetValue(TDataBase));
                }
            }

            for (byte index = 0; index < KeyS.Length; index++)
            {
                WhereName += WhereName.Length > 0 ? " and " + KeyS[index] + " = @" + KeyS[index] : " Where " + KeyS[index] + " = @" + KeyS[index];
            }

            SelSql += FieldValueName + WhereName;
            ExeSQL(SelSql, TSqlParameter);
        }
コード例 #2
0
        /// <summary>
        ///  添加一个对象
        /// </summary>
        /// <param name="TDataBase"></param>
        public void AddOnlyObject(IDataBase TDataBase)
        {
            DataObjectAttribute TDataObject = TDataBase.GetType().GetCustomAttributes(typeof(DataObjectAttribute), false)[0] as DataObjectAttribute;

            string[] KeyS      = TDataObject.KeyS.Split(new char[] { ',' });
            string   TableName = TDataObject.Table;

            System.Reflection.FieldInfo[]        mFieldInfo    = TDataBase.GetType().GetFields();
            System.Data.SqlClient.SqlParameter[] TSqlParameter = new SqlParameter[mFieldInfo.Length];

            string SelSql    = "insert  into " + TableName + " ";
            string FieldName = string.Empty;
            string ValueName = string.Empty;

            for (byte index = 0; index < mFieldInfo.Length; index++)
            {
                if (mFieldInfo[index].GetCustomAttributes(typeof(FieldObjectAttribute), false).Length != 0)
                {
                    FieldObjectAttribute TFieldObject = mFieldInfo[index].GetCustomAttributes(typeof(FieldObjectAttribute), false)[0] as FieldObjectAttribute;
                    if (TFieldObject.Type == 0)
                    {
                        FieldName           += FieldName.Length > 0 ? "," + mFieldInfo[index].Name : mFieldInfo[index].Name;
                        ValueName           += ValueName.Length > 0 ? "," + "@" + mFieldInfo[index].Name : "@" + mFieldInfo[index].Name;
                        TSqlParameter[index] = new SqlParameter("@" + mFieldInfo[index].Name, System.Guid.NewGuid().ToString());
                    }
                    else if (TFieldObject.Type == 1)
                    {
                        // 此字段为自增列放弃
                    }
                }
                else
                {
                    FieldName           += FieldName.Length > 0 ? "," + mFieldInfo[index].Name : mFieldInfo[index].Name;
                    ValueName           += ValueName.Length > 0 ? "," + "@" + mFieldInfo[index].Name : "@" + mFieldInfo[index].Name;
                    TSqlParameter[index] = new SqlParameter("@" + mFieldInfo[index].Name, TDataBase.GetType().GetField(mFieldInfo[index].Name).GetValue(TDataBase));
                }
            }

            SelSql += " (" + FieldName + ") VALUES (" + ValueName + ")";
            ExeSQL(SelSql, TSqlParameter);
        }