private bool ExecuteSqlScript(IEnumerable <string> sqlScriptFilePaths)
        {
            bool connetSuccess = true;

            try
            {
                DbUtility dbUtility = DbUtilityCreator.GetDefaultDbUtility();
                if (dbUtility.DbProviderType == DbProviderType.MySql)
                {
                    foreach (var sqlScriptFilePath in sqlScriptFilePaths)
                    {
                        MySqlDataBaseHelper.ExecuteMySqlScriptFile(sqlScriptFilePath, dbUtility.ConnectionString);
                    }
                }
                else
                {
                    //TODO:处理其它数据库执行脚本代码
                }
            }
            catch (Exception)
            {
                connetSuccess = false;
                throw;
            }

            return(connetSuccess);
        }
        private bool CreataDataSqlScript(IEnumerable <string> sqlScriptFilePaths)
        {
            bool connetSuccess = true;

            try
            {
                DbUtility dbUtility = DbUtilityCreator.GetDefaultDbUtility();
                if (dbUtility.DbProviderType == DbProviderType.MySql)
                {
                    //创建数据库脚本时,还没有数据库存在,所以
                    //connectionString不能存在数据库名,否则执行脚本不成功。
                    string connectionString = dbUtility.ConnectionString.Replace(dbUtility.SqlconModel.DBName, "");

                    foreach (var sqlScriptFilePath in sqlScriptFilePaths)
                    {
                        MySqlDataBaseHelper.ExecuteMySqlScriptFile(sqlScriptFilePath, connectionString);
                    }
                }
                else
                {
                    //TODO:处理其它数据库执行脚本代码
                }
            }
            catch (Exception)
            {
                connetSuccess = false;
                throw;
            }

            return(connetSuccess);
        }
Exemplo n.º 3
0
        private string CreateFullSqlParameter()
        {
            var code = new StringBuilder();

            code.Append($@"

        /// <summary>
        /// 设置插入数据的命令
        /// </summary>
        /// <param name=""entity"">实体对象</param>
        /// <param name=""cmd"">命令</param>
        /// <returns>返回真说明要取主键</returns>
        private void CreateFullSqlParameter({this.Entity.EntityName} entity, MySqlCommand cmd)
        {{");

            var isFirstNull = true;

            foreach (var field in PublishDbFields.OrderBy(p => p.Index))
            {
                code.AppendFormat(@"
            //{2:D2}:{0}({1})", field.Caption, field.Name, field.Index + 1);
                if (!string.IsNullOrWhiteSpace(field.CustomType))
                {
                    code.Append($@"
            cmd.Parameters.Add(new MySqlParameter(""{field.Name}"",MySqlDbType.{MySqlDataBaseHelper.ToSqlDbType(field)}){{ Value = (int)entity.{field.Name}}});");
                    continue;
                }
                switch (field.CsType)
                {
                case "String":
                case "string":
                    code.Append($@"
            {(isFirstNull ? "var " : "")}isNull = string.IsNullOrWhiteSpace({CustomName(field, "entity.")});
            {(isFirstNull ? "var " : "")}parameter = new MySqlParameter(""{field.Name}"",MySqlDbType.{MySqlDataBaseHelper.ToSqlDbType(field)} , isNull ? 10 : ({CustomName(field, "entity.")}).Length);
            if(isNull)
                parameter.Value = DBNull.Value;
            else
                parameter.Value = {PropertyName(field, "entity.")};
            cmd.Parameters.Add(parameter);");
                    break;

                case "byte[]":
                case "Byte[]":
                    code.Append($@"
            {(isFirstNull ? "var " : "")}isNull = {CustomName(field, "entity.")} == null || {CustomName(field, "entity.")}.Length == 0;
            {(isFirstNull ? "var " : "")}parameter = new MySqlParameter(""{field.Name}"",MySqlDbType.{MySqlDataBaseHelper.ToSqlDbType(field)} , isNull ? 10 : {CustomName(field, "entity.")}.Length);
            if(isNull)
                parameter.Value = DBNull.Value;
            else
                parameter.Value = {PropertyName(field, "entity.")};
            cmd.Parameters.Add(parameter);");
                    break;

                case "DateTime":
                    field.DbNullable = true;
                    if (field.Nullable)
                    {
                        code.Append($@"
            {(isFirstNull ? "var " : "")}isNull = entity.{field.Name} == null || entity.{field.Name}.Value.Year < 1900;
            {(isFirstNull ? "var " : "")}parameter = new MySqlParameter(""{field.Name}"",MySqlDbType.{MySqlDataBaseHelper.ToSqlDbType(field)});
            if(isNull)
                parameter.Value = DBNull.Value;
            else
                parameter.Value = entity.{field.Name};
            cmd.Parameters.Add(parameter);");
                    }
                    else
                    {
                        code.Append($@"
            {(isFirstNull ? "var " : "")}isNull = entity.{field.Name}.Year < 1900;
            {(isFirstNull ? "var " : "")}parameter = new MySqlParameter(""{field.Name}"",MySqlDbType.{MySqlDataBaseHelper.ToSqlDbType(field)});
            if(isNull)
                parameter.Value = DBNull.Value;
            else
                parameter.Value = entity.{field.Name};
            cmd.Parameters.Add(parameter);");
                    }
                    break;

                case "bool":
                    if (!field.Nullable)
                    {
                        code.Append($@"
            cmd.Parameters.Add(new MySqlParameter(""{field.Name}"",MySqlDbType.Byte) {{ Value = entity.{field.Name} ? (byte)1 : (byte)0 }});");
                        continue;
                    }
                    code.Append($@"
            {(isFirstNull ? "var " : "")}isNull = entity.{field.Name} == null;
            {(isFirstNull ? "var " : "")}parameter = new MySqlParameter(""{field.Name}"",MySqlDbType.{MySqlDataBaseHelper.ToSqlDbType(field)});
            if(isNull)
                parameter.Value = DBNull.Value;
            else
                parameter.Value = entity.{field.Name} ? (byte)1 : (byte)0;
            cmd.Parameters.Add(parameter);");
                    break;

                default:
                    if (!field.Nullable)
                    {
                        code.Append($@"
            cmd.Parameters.Add(new MySqlParameter(""{field.Name}"",MySqlDbType.{MySqlDataBaseHelper.ToSqlDbType(field)}){{ Value = entity.{field.Name}}});");
                        continue;
                    }
                    code.Append($@"
            {(isFirstNull ? "var " : "")}isNull = entity.{field.Name} == null;
            {(isFirstNull ? "var " : "")}parameter = new MySqlParameter(""{field.Name}"",MySqlDbType.{MySqlDataBaseHelper.ToSqlDbType(field)});
            if(isNull)
                parameter.Value = DBNull.Value;
            else
                parameter.Value = {PropertyName(field, "entity.")};
            cmd.Parameters.Add(parameter);");
                    break;
                }
                if (isFirstNull)
                {
                    isFirstNull = false;
                }
            }
            code.Append(@"
        }");
            return(code.ToString());
        }