using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using CallCenterApi.DB;

namespace CallCenterApi.DAL
{
    /// <summary>
    /// 数据访问类:T_Form_EntityInfo
    /// </summary>
    public partial class T_Form_EntityInfo
    {
        public T_Form_EntityInfo()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int F_EntityId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_Form_EntityInfo");
            strSql.Append(" where F_EntityId=@F_EntityId");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_EntityId", SqlDbType.Int,4)
            };
            parameters[0].Value = F_EntityId;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.T_Form_EntityInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Form_EntityInfo(");
            strSql.Append("F_Name,F_DBTableName,F_IsActivity,F_Des,F_LinkTableName,F_LinkFieldName,F_CreateSQL,F_CreateOn,F_ModifySQL,F_LastModifyOn)");
            strSql.Append(" values (");
            strSql.Append("@F_Name,@F_DBTableName,@F_IsActivity,@F_Des,@F_LinkTableName,@F_LinkFieldName,@F_CreateSQL,@F_CreateOn,@F_ModifySQL,@F_LastModifyOn)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_Name", SqlDbType.NVarChar,64),
                    new SqlParameter("@F_DBTableName", SqlDbType.NVarChar,64),
                    new SqlParameter("@F_IsActivity", SqlDbType.SmallInt,2),
                    new SqlParameter("@F_Des", SqlDbType.NVarChar,800),
                    new SqlParameter("@F_LinkTableName", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_LinkFieldName", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_CreateSQL", SqlDbType.Text),
                    new SqlParameter("@F_CreateOn", SqlDbType.DateTime),
                    new SqlParameter("@F_ModifySQL", SqlDbType.Text),
                    new SqlParameter("@F_LastModifyOn", SqlDbType.DateTime)};
            parameters[0].Value = model.F_Name;
            parameters[1].Value = model.F_DBTableName;
            parameters[2].Value = model.F_IsActivity;
            parameters[3].Value = model.F_Des;
            parameters[4].Value = model.F_LinkTableName;
            parameters[5].Value = model.F_LinkFieldName;
            parameters[6].Value = model.F_CreateSQL;
            parameters[7].Value = model.F_CreateOn;
            parameters[8].Value = model.F_ModifySQL;
            parameters[9].Value = model.F_LastModifyOn;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.T_Form_EntityInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Form_EntityInfo set ");
            strSql.Append("F_Name=@F_Name,");
            strSql.Append("F_DBTableName=@F_DBTableName,");
            strSql.Append("F_IsActivity=@F_IsActivity,");
            strSql.Append("F_Des=@F_Des,");
            strSql.Append("F_LinkTableName=@F_LinkTableName,");
            strSql.Append("F_LinkFieldName=@F_LinkFieldName,");
            strSql.Append("F_CreateSQL=@F_CreateSQL,");
            strSql.Append("F_CreateOn=@F_CreateOn,");
            strSql.Append("F_ModifySQL=@F_ModifySQL,");
            strSql.Append("F_LastModifyOn=@F_LastModifyOn");
            strSql.Append(" where F_EntityId=@F_EntityId");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_Name", SqlDbType.NVarChar,64),
                    new SqlParameter("@F_DBTableName", SqlDbType.NVarChar,64),
                    new SqlParameter("@F_IsActivity", SqlDbType.SmallInt,2),
                    new SqlParameter("@F_Des", SqlDbType.NVarChar,800),
                    new SqlParameter("@F_LinkTableName", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_LinkFieldName", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_CreateSQL", SqlDbType.Text),
                    new SqlParameter("@F_CreateOn", SqlDbType.DateTime),
                    new SqlParameter("@F_ModifySQL", SqlDbType.Text),
                    new SqlParameter("@F_LastModifyOn", SqlDbType.DateTime),
                    new SqlParameter("@F_EntityId", SqlDbType.Int,4)};
            parameters[0].Value = model.F_Name;
            parameters[1].Value = model.F_DBTableName;
            parameters[2].Value = model.F_IsActivity;
            parameters[3].Value = model.F_Des;
            parameters[4].Value = model.F_LinkTableName;
            parameters[5].Value = model.F_LinkFieldName;
            parameters[6].Value = model.F_CreateSQL;
            parameters[7].Value = model.F_CreateOn;
            parameters[8].Value = model.F_ModifySQL;
            parameters[9].Value = model.F_LastModifyOn;
            parameters[10].Value = model.F_EntityId;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int F_EntityId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Form_EntityInfo ");
            strSql.Append(" where F_EntityId=@F_EntityId");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_EntityId", SqlDbType.Int,4)
            };
            parameters[0].Value = F_EntityId;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string F_EntityIdlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Form_EntityInfo ");
            strSql.Append(" where F_EntityId in (" + F_EntityIdlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.T_Form_EntityInfo GetModel(int F_EntityId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 F_EntityId,F_Name,F_DBTableName,F_IsActivity,F_Des,F_LinkTableName,F_LinkFieldName,F_CreateSQL,F_CreateOn,F_ModifySQL,F_LastModifyOn from T_Form_EntityInfo ");
            strSql.Append(" where F_EntityId=@F_EntityId");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_EntityId", SqlDbType.Int,4)
            };
            parameters[0].Value = F_EntityId;

            Model.T_Form_EntityInfo model = new Model.T_Form_EntityInfo();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["F_EntityId"] != null && ds.Tables[0].Rows[0]["F_EntityId"].ToString() != "")
                {
                    model.F_EntityId = int.Parse(ds.Tables[0].Rows[0]["F_EntityId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["F_Name"] != null && ds.Tables[0].Rows[0]["F_Name"].ToString() != "")
                {
                    model.F_Name = ds.Tables[0].Rows[0]["F_Name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["F_DBTableName"] != null && ds.Tables[0].Rows[0]["F_DBTableName"].ToString() != "")
                {
                    model.F_DBTableName = ds.Tables[0].Rows[0]["F_DBTableName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["F_IsActivity"] != null && ds.Tables[0].Rows[0]["F_IsActivity"].ToString() != "")
                {
                    model.F_IsActivity = int.Parse(ds.Tables[0].Rows[0]["F_IsActivity"].ToString());
                }
                if (ds.Tables[0].Rows[0]["F_Des"] != null && ds.Tables[0].Rows[0]["F_Des"].ToString() != "")
                {
                    model.F_Des = ds.Tables[0].Rows[0]["F_Des"].ToString();
                }
                if (ds.Tables[0].Rows[0]["F_LinkTableName"] != null && ds.Tables[0].Rows[0]["F_LinkTableName"].ToString() != "")
                {
                    model.F_LinkTableName = ds.Tables[0].Rows[0]["F_LinkTableName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["F_LinkFieldName"] != null && ds.Tables[0].Rows[0]["F_LinkFieldName"].ToString() != "")
                {
                    model.F_LinkFieldName = ds.Tables[0].Rows[0]["F_LinkFieldName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["F_CreateSQL"] != null && ds.Tables[0].Rows[0]["F_CreateSQL"].ToString() != "")
                {
                    model.F_CreateSQL = ds.Tables[0].Rows[0]["F_CreateSQL"].ToString();
                }
                if (ds.Tables[0].Rows[0]["F_CreateOn"] != null && ds.Tables[0].Rows[0]["F_CreateOn"].ToString() != "")
                {
                    model.F_CreateOn = DateTime.Parse(ds.Tables[0].Rows[0]["F_CreateOn"].ToString());
                }
                if (ds.Tables[0].Rows[0]["F_ModifySQL"] != null && ds.Tables[0].Rows[0]["F_ModifySQL"].ToString() != "")
                {
                    model.F_ModifySQL = ds.Tables[0].Rows[0]["F_ModifySQL"].ToString();
                }
                if (ds.Tables[0].Rows[0]["F_LastModifyOn"] != null && ds.Tables[0].Rows[0]["F_LastModifyOn"].ToString() != "")
                {
                    model.F_LastModifyOn = DateTime.Parse(ds.Tables[0].Rows[0]["F_LastModifyOn"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select F_EntityId,F_Name,F_DBTableName,F_IsActivity,F_Des,F_LinkTableName,F_LinkFieldName,F_CreateSQL,F_CreateOn,F_ModifySQL,F_LastModifyOn ");
            strSql.Append(" FROM T_Form_EntityInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" F_EntityId,F_Name,F_DBTableName,F_IsActivity,F_Des,F_LinkTableName,F_LinkFieldName,F_CreateSQL,F_CreateOn,F_ModifySQL,F_LastModifyOn ");
            strSql.Append(" FROM T_Form_EntityInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM T_Form_EntityInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.F_EntityId desc");
            }
            strSql.Append(")AS Row, T.*  from T_Form_EntityInfo T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "T_Form_EntityInfo";
            parameters[1].Value = "F_EntityId";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int AddTran(Model.T_Form_EntityInfo model)
        {
            try
            {
                int res = -1;
                StringBuilder strSql = new StringBuilder();
                StringBuilder strFildSql = new StringBuilder();
                using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString))
                {
                    if (conn.State.ToString() != "Open")
                        conn.Open();

                    ///设置隔离级别,启动数据库事务
                    SqlTransaction trans = conn.BeginTransaction(IsolationLevel.ReadCommitted);

                    SqlCommand cmd = new SqlCommand();
                    try
                    {
                        strSql.Append("Declare @err int;declare @res int; declare @F_EntityId int;set @F_EntityId=0; set @err=0;set @res=0;");
                        //判断系统是否存在此表
                        strSql.Append("if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[" + model.F_DBTableName + "]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)");
                        strSql.Append("begin set @res=-2 end else begin ");

                        #region 实体信息

                        strSql.Append("insert into T_Form_EntityInfo(");
                        strSql.Append("F_Name,F_DBTableName,F_IsActivity,F_Des,F_LinkTableName,F_LinkFieldName,F_CreateSQL,F_CreateOn,F_ModifySQL,F_LastModifyOn)");
                        strSql.Append(" values (");
                        strSql.Append("@F_Name,@F_DBTableName,@F_IsActivity,@F_Des,@F_LinkTableName,@F_LinkFieldName,@F_CreateSQL,@F_CreateOn,@F_ModifySQL,@F_LastModifyOn)");
                        strSql.Append(";select @F_EntityId=@@IDENTITY;SELECT @err=@@ERROR;");
                        SqlParameter[] parameters = {
                    new SqlParameter("@F_Name", SqlDbType.NVarChar,64),
                    new SqlParameter("@F_DBTableName", SqlDbType.NVarChar,64),
                    new SqlParameter("@F_IsActivity", SqlDbType.SmallInt,2),
                    new SqlParameter("@F_Des", SqlDbType.NVarChar,800),
                    new SqlParameter("@F_LinkTableName", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_LinkFieldName", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_CreateSQL", SqlDbType.Text),
                    new SqlParameter("@F_CreateOn", SqlDbType.DateTime),
                    new SqlParameter("@F_ModifySQL", SqlDbType.Text),
                    new SqlParameter("@F_LastModifyOn", SqlDbType.DateTime)};
                        parameters[0].Value = model.F_Name;
                        parameters[1].Value = model.F_DBTableName;
                        parameters[2].Value = model.F_IsActivity;
                        parameters[3].Value = model.F_Des;
                        parameters[4].Value = model.F_LinkTableName;
                        parameters[5].Value = model.F_LinkFieldName;
                        parameters[6].Value = model.F_CreateSQL;
                        parameters[7].Value = model.F_CreateOn;
                        parameters[8].Value = model.F_ModifySQL;
                        parameters[9].Value = model.F_LastModifyOn;

                        #endregion

                        #region 属性信息

                        if (model.EntityAttributeItems != null)
                        {
                            int i = 0;
                            foreach (Model.T_Form_EntityAttribute attributemodel in model.EntityAttributeItems)
                            {
                                strSql.Append("insert into T_Form_EntityAttribute(");
                                strSql.Append("F_TypeId,F_DBTypeName,F_Name,F_DBFieldlName,F_Length,F_IsNullable,F_EntityId,F_DBTableName,F_DefaultValue,F_ColumnNumber,F_ValidForUpdateAPI,F_ValidForReadAPI,F_ValidForCreateAPI,F_VisibleToPlatform,F_IsPKAttribute,F_IsCustomField,F_DisplayMask,F_IsSortAttribute,F_PrecisionValue,F_IsIdentity,F_AppDefaultValue,F_Locked,F_MaxLength,F_MinValue,F_MaxValue,F_LookupKey,F_LookupStyle,F_IsLookup,F_Des)");
                                strSql.Append(" values (");
                                strSql.Append("@F_TypeId_" + i.ToString() + ",");
                                strSql.Append("@F_DBTypeName_" + i.ToString() + ",");
                                strSql.Append("@F_Name_" + i.ToString() + ",");
                                strSql.Append("@F_DBFieldlName_" + i.ToString() + ",");
                                strSql.Append("@F_Length_" + i.ToString() + ",");
                                strSql.Append("@F_IsNullable_" + i.ToString() + ",");
                                strSql.Append("@F_EntityId,");
                                strSql.Append("@F_DBTableName_" + i.ToString() + ",");
                                strSql.Append("@F_DefaultValue_" + i.ToString() + ",");
                                strSql.Append("@F_ColumnNumber_" + i.ToString() + ",");
                                strSql.Append("@F_ValidForUpdateAPI_" + i.ToString() + ",");
                                strSql.Append("@F_ValidForReadAPI_" + i.ToString() + ",");
                                strSql.Append("@F_ValidForCreateAPI_" + i.ToString() + ",");
                                strSql.Append("@F_VisibleToPlatform_" + i.ToString() + ",");
                                strSql.Append("@F_IsPKAttribute_" + i.ToString() + ",");
                                strSql.Append("@F_IsCustomField_" + i.ToString() + ",");
                                strSql.Append("@F_DisplayMask_" + i.ToString() + ",");
                                strSql.Append("@F_IsSortAttribute_" + i.ToString() + ",");
                                strSql.Append("@F_PrecisionValue_" + i.ToString() + ",");
                                strSql.Append("@F_IsIdentity_" + i.ToString() + ",");
                                strSql.Append("@F_AppDefaultValue_" + i.ToString() + ",");
                                strSql.Append("@F_Locked_" + i.ToString() + ",");
                                strSql.Append("@F_MaxLength_" + i.ToString() + ",");
                                strSql.Append("@F_MinValue_" + i.ToString() + ",");
                                strSql.Append("@F_MaxValue_" + i.ToString() + ",");
                                strSql.Append("@F_LookupKey_" + i.ToString() + ",");
                                strSql.Append("@F_LookupStyle_" + i.ToString() + ",");
                                strSql.Append("@F_IsLookup_" + i.ToString() + ",");
                                strSql.Append("@F_Des_" + i.ToString() + ")");

                                SqlParameter[] parametersAttribute = {
                    new SqlParameter("@F_TypeId_"+i.ToString(), SqlDbType.Int,4),
                    new SqlParameter("@F_DBTypeName_"+i.ToString(), SqlDbType.NVarChar,50),
                    new SqlParameter("@F_Name_"+i.ToString(), SqlDbType.NVarChar,50),
                    new SqlParameter("@F_DBFieldlName_"+i.ToString(), SqlDbType.NVarChar,50),
                    new SqlParameter("@F_Length_"+i.ToString(), SqlDbType.Int,4),
                    new SqlParameter("@F_IsNullable_"+i.ToString(), SqlDbType.Bit,1),
                    new SqlParameter("@F_DBTableName_"+i.ToString(), SqlDbType.NVarChar,50),
                    new SqlParameter("@F_DefaultValue_"+i.ToString(), SqlDbType.NVarChar,50),
                    new SqlParameter("@F_ColumnNumber_"+i.ToString(), SqlDbType.Int,4),
                    new SqlParameter("@F_ValidForUpdateAPI_"+i.ToString(), SqlDbType.Bit,1),
                    new SqlParameter("@F_ValidForReadAPI_"+i.ToString(), SqlDbType.Bit,1),
                    new SqlParameter("@F_ValidForCreateAPI_"+i.ToString(), SqlDbType.Bit,1),
                    new SqlParameter("@F_VisibleToPlatform_"+i.ToString(), SqlDbType.Bit,1),
                    new SqlParameter("@F_IsPKAttribute_"+i.ToString(), SqlDbType.Bit,1),
                    new SqlParameter("@F_IsCustomField_"+i.ToString(), SqlDbType.Bit,1),
                    new SqlParameter("@F_DisplayMask_"+i.ToString(), SqlDbType.Int,4),
                    new SqlParameter("@F_IsSortAttribute_"+i.ToString(), SqlDbType.Bit,1),
                    new SqlParameter("@F_PrecisionValue_"+i.ToString(), SqlDbType.Int,4),
                    new SqlParameter("@F_IsIdentity_"+i.ToString(), SqlDbType.Bit,1),
                    new SqlParameter("@F_AppDefaultValue_"+i.ToString(), SqlDbType.Int,4),
                    new SqlParameter("@F_Locked_"+i.ToString(), SqlDbType.Bit,1),
                    new SqlParameter("@F_MaxLength_"+i.ToString(), SqlDbType.Int,4),
                    new SqlParameter("@F_MinValue_"+i.ToString(), SqlDbType.Float,8),
                    new SqlParameter("@F_MaxValue_"+i.ToString(), SqlDbType.Float,8),
                    new SqlParameter("@F_LookupKey_"+i.ToString(), SqlDbType.NVarChar,50),
                    new SqlParameter("@F_LookupStyle_"+i.ToString(), SqlDbType.NVarChar,50),
                    new SqlParameter("@F_IsLookup_"+i.ToString(), SqlDbType.Bit,1),
                    new SqlParameter("@F_Des_"+i.ToString(), SqlDbType.NVarChar,500)};
                                parametersAttribute[0].Value = attributemodel.F_TypeId;
                                parametersAttribute[1].Value = attributemodel.F_DBTypeName;
                                parametersAttribute[2].Value = attributemodel.F_Name;
                                parametersAttribute[3].Value = attributemodel.F_DBFieldlName;
                                parametersAttribute[4].Value = attributemodel.F_Length;
                                parametersAttribute[5].Value = attributemodel.F_IsNullable;
                                parametersAttribute[6].Value = attributemodel.F_DBTableName;
                                parametersAttribute[7].Value = attributemodel.F_DefaultValue;
                                parametersAttribute[8].Value = attributemodel.F_ColumnNumber;
                                parametersAttribute[9].Value = attributemodel.F_ValidForUpdateAPI;
                                parametersAttribute[10].Value = attributemodel.F_ValidForReadAPI;
                                parametersAttribute[11].Value = attributemodel.F_ValidForCreateAPI;
                                parametersAttribute[12].Value = attributemodel.F_VisibleToPlatform;
                                parametersAttribute[13].Value = attributemodel.F_IsPKAttribute;
                                parametersAttribute[14].Value = attributemodel.F_IsCustomField;
                                parametersAttribute[15].Value = attributemodel.F_DisplayMask;
                                parametersAttribute[16].Value = attributemodel.F_IsSortAttribute;
                                parametersAttribute[17].Value = attributemodel.F_PrecisionValue;
                                parametersAttribute[18].Value = attributemodel.F_IsIdentity;
                                parametersAttribute[19].Value = attributemodel.F_AppDefaultValue;
                                parametersAttribute[20].Value = attributemodel.F_Locked;
                                parametersAttribute[21].Value = attributemodel.F_MaxLength;
                                parametersAttribute[22].Value = attributemodel.F_MinValue;
                                parametersAttribute[23].Value = attributemodel.F_MaxValue;
                                parametersAttribute[24].Value = attributemodel.F_LookupKey;
                                parametersAttribute[25].Value = attributemodel.F_LookupStyle;
                                parametersAttribute[26].Value = attributemodel.F_IsLookup;
                                parametersAttribute[27].Value = attributemodel.F_Des;


                                strSql.Append(" SELECT @err=@err+@@ERROR; ");

                                string isnull = " NULL ";
                                string ispkey = "";
                                string defaultvalue = "";
                                string identity = "";
                                string fieldname = attributemodel.F_DBTypeName;
                                if (attributemodel.F_Length != null)
                                {
                                    fieldname = fieldname + "(" + attributemodel.F_Length.ToString() + ")";
                                }
                                if (attributemodel.F_IsNullable)
                                {
                                    isnull = " NULL ";
                                }
                                else
                                {
                                    isnull = " NOT NULL ";
                                }

                                if (attributemodel.F_IsPKAttribute)
                                {
                                    ispkey = " primary key ";
                                }
                                else
                                {
                                    ispkey = "";
                                }

                                if (attributemodel.F_IsIdentity)
                                {
                                    identity = " IDENTITY(1,1) ";
                                }
                                else
                                {
                                    identity = "";
                                }

                                if (!string.IsNullOrEmpty(attributemodel.F_DefaultValue))
                                {
                                    defaultvalue = " default(" + attributemodel.F_DefaultValue.Trim() + ") ";
                                }

                                strFildSql.Append("[" + attributemodel.F_DBFieldlName + "] " + fieldname + isnull + ispkey + identity + defaultvalue + " ,");

                                //遍历绑定参数
                                foreach (SqlParameter parm in parametersAttribute)
                                {
                                    if ((parm.Direction == ParameterDirection.InputOutput || parm.Direction == ParameterDirection.Input) &&
                                (parm.Value == null))
                                    {
                                        parm.Value = DBNull.Value;
                                    }
                                    cmd.Parameters.Add(parm);
                                }

                                i++;
                            }
                        }

                        #endregion

                        #region 创建表

                        strFildSql.Append("#@!");
                        strSql.Append("CREATE TABLE [dbo].[" + model.F_DBTableName + "] (");
                        strSql.Append(strFildSql.ToString().Replace(",#@!", "").Replace("#@!", ""));
                        strSql.Append(")");

                        strSql.Append(" SELECT @err=@err+@@ERROR; ");

                        #endregion

                        parameters[6].Value = "CREATE TABLE [dbo].[" + model.F_DBTableName + "] (" + strFildSql.ToString().Replace(",#@!", "").Replace("#@!", "") + ")";

                        foreach (SqlParameter parm in parameters)
                        {
                            if ((parm.Direction == ParameterDirection.InputOutput || parm.Direction == ParameterDirection.Input) &&
                        (parm.Value == null))
                            {
                                parm.Value = DBNull.Value;
                            }
                            cmd.Parameters.Add(parm);
                        }

                        #region 执行命令

                        cmd.CommandType = CommandType.Text;
                        cmd.Connection = trans.Connection;
                        cmd.Transaction = trans;

                        strSql.Append(" end ");

                        cmd.CommandText = strSql.Append("SELECT @err,@res").ToString();

                        using (SqlDataReader rdr = cmd.ExecuteReader())
                        {
                            rdr.Read();
                            // 如果错误的数量不为零, 抛出异常终止操作
                            if (rdr.GetInt32(0) != 0)
                            {
                                //throw new ApplicationException("数据插入错误 —— 回滚原因");
                                trans.Rollback();
                                res = -1;
                            }
                            else
                            {
                                res = rdr.GetInt32(1);
                            }
                        }
                        ///提交事务
                        trans.Commit();
                        cmd.Parameters.Clear();

                        #endregion
                    }
                    catch
                    {
                        res = -1;
                        ///事务回滚
                        trans.Rollback();
                        //throw new ApplicationException(e.Message);
                    }
                    finally
                    {
                        strSql = null;
                        conn.Close();
                        conn.Dispose();
                    }
                }
                return res;
            }
            catch
            {
                return -1;
            }
        }
    }
}