Exemplo n.º 1
0
 /// <summary>
 /// 获取分页数据
 /// </summary>
 public DatabaseResultModel<List<TDemoModel>> getListData(TDemoModel searchModel)
 {
     var result = new DatabaseResultModel<List<TDemoModel>>();
     try
     {
         using (databaseConnector = getDatabaseConnector())
         using (databaseExecutor = new Executor(databaseConnector))
         {
             databaseScriptModel = new DatabaseScriptModel();
             databaseScriptModel.command = (new TDemoScript()).getListData(searchModel);
             result.data = databaseExecutor.sqlQueryToList<TDemoModel>(databaseScriptModel);
         }
     }
     catch (Exception ex) { result.exception = ex; }
     return result;
 }
Exemplo n.º 2
0
 /// <summary>
 /// 删除
 /// </summary>
 public DatabaseResultModel<bool> delete(TDemoModel model)
 {
     var result = new DatabaseResultModel<bool>();
     try
     {
         using (databaseConnector = getDatabaseConnector())
         using (databaseExecutor = new Executor(databaseConnector))
         {
             databaseScriptModel = new DatabaseScriptModel();
             databaseScriptModel.command = (new TDemoScript()).delete(model);
             executeSqlCommand(new TDemoScript());
             result.data = true;
         }
     }
     catch (Exception ex) { result.exception = ex; result.data = false; }
     return result;
 }
 /// <summary>
 /// 获取Sql执行出错信息
 /// </summary>
 /// <param name="id">方法ID</param>
 /// <param name="scriptModel">DatabaseScriptModel对象</param>
 /// <param name="exception">错误信息</param>
 /// <returns>Sql执行出错信息</returns>
 public static Exception getSqlException(string id, DatabaseScriptModel scriptModel, Exception exception)
 {
     StringBuilder sb = new StringBuilder();
     sb.AppendLine("▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼");
     sb.AppendLine("ID:" + id);
     sb.AppendLine("====================Sql Start=======================");
     sb.AppendLine(scriptModel.command);
     sb.AppendLine("====================Sql End=======================");
     sb.AppendLine("Error Msg:");
     sb.AppendLine(exception.GetBaseException().Message);
     sb.AppendLine("Stack:");
     sb.AppendLine(exception.GetBaseException().StackTrace);
     sb.AppendLine("Ect:");
     sb.AppendLine(exception.Message.ToString());
     sb.AppendLine(exception.Source);
     sb.AppendLine(exception.StackTrace);
     sb.AppendLine("▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲");
     return new Exception(sb.ToString());
 }
Exemplo n.º 4
0
        /// <summary>
        /// 将DataTable中的数据大批量高速写入至数据库,目前只支持字符、数字、时间、布尔类型。
        /// </summary>
        /// <param name="sqlDataTable">DataTable数据源</param>
        public void executeMySQLBulkSqlCommand(DataTable sqlDataTable)
        {
            try
            {
                int rowIndex = 0;
                int columnIndex = 0;
                DatabaseScriptModel scriptModel = new DatabaseScriptModel();
                StringBuilder sb = new StringBuilder();
                sb.Append("INSERT INTO `" + sqlDataTable.TableName.Replace("`", "``") + "`" );

                //生成字段
                sb.Append("(");
                columnIndex = 0;
                foreach (DataColumn dc in sqlDataTable.Columns)
                {
                    sb.Append((columnIndex > 0 ? "," : ""));
                    sb.Append("`" + dc.ColumnName.Replace("`", "``") + "`");
                    columnIndex++;
                }
                sb.Append(")");

                //填充Values
                sb.Append(" VALUES ");
                foreach (DataRow dr in sqlDataTable.Rows)
                {
                    columnIndex = 0;
                    sb.Append((rowIndex > 0 ? "\r\n," : "" ) + "(");
                    foreach (DataColumn dc in sqlDataTable.Columns)
                    {
                        //NULL的时候
                        if (dr[dc.ColumnName] == null)
                        {
                            sb.Append((columnIndex > 0 ? "," : "") + "NULL");
                            columnIndex++;
                            continue;
                        }

                        //非NULL时,根据数据类型填充
                        sb.Append((columnIndex > 0 ? "," : ""));
                        switch (dc.DataType.Name.ToLower())
                        {
                            case "bool":
                            case "boolean":
                                sb.Append(dr[dc.ColumnName].ToString());
                                break;
                            default:
                                sb.Append("'" + dr[dc.ColumnName].ToString().Replace("'", "''") + "'");
                                break;
                        }
                        columnIndex++;
                    }
                    sb.Append(")");
                    rowIndex++;
                }
                sb.Append(";");
                scriptModel.command = sb.ToString();

                base.executeSqlCommand(new List<DatabaseScriptModel> { scriptModel });
            }
            catch (Exception ex)
            {
                throw DatabaseException.getSqlException("executeMySQLBulkSqlCommand", sqlDataTable, ex);
            }
        }