/// <summary> /// 解析数据库连接字符串,去除字符串内的 version、|datadirectory| 等参数。 /// </summary> /// <param name="constr">配置的源连接字符串。</param> /// <returns>可用于进行数据库连接的字符串。</returns> private string ParseConnectionString(string constr) { var builder = new StringBuilder(); //循环每节属性 var args = constr.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries); foreach (string par in args) { var segs = par.Split(new[] { '=' }, StringSplitOptions.RemoveEmptyEntries); if (segs.Length == 0) { continue; } var name = segs[0].Trim(); var value = segs[1].Trim(); if (!ParseParameter(name, value)) { DbUtility.ParseDataDirectory(ref value); builder.AppendFormat("{0}={1};", name, value); } Properties.Add(name.ToLower(), value); } return(builder.ToString()); }
/// <summary> /// 解析数据库连接字符串,去除字符串内的 version、|datadirectory| 等参数。 /// </summary> /// <param name="constr">配置的源连接字符串。</param> /// <returns>可用于进行数据库连接的字符串。</returns> private string ParseConnectionString(string constr) { var builder = new StringBuilder(); var index = 0; var name = string.Empty; var value = string.Empty; var last = false; var quote = false; for (var i = 0; i < constr.Length; i++) { if (constr[i] == '"' || constr[i] == '\'') { quote = !quote; } else if (constr[i] == '=' && !quote) { name = constr.Substring(index, i - index); index = i + 1; } else if (constr[i] == ';' && !quote) { value = constr.Substring(index, i - index); if (!ParseParameter(name, value)) { value = DbUtility.ResolveFullPath(value); builder.AppendFormat("{0}={1};", name, value); } Properties.Add(name.ToLower(), value); index = i + 1; last = true; } else { last = false; } } if (!last) { value = constr.Substring(index); if (string.IsNullOrEmpty(name)) { builder.Append(value); } else if (!ParseParameter(name, value)) { value = DbUtility.ResolveFullPath(value); builder.AppendFormat("{0}={1};", name, value); } Properties.Add(name.ToLower(), value); } return(builder.ToString()); }
private SqlCommand GetCommand(CommandContext context) { var cullingOrderBy = DbUtility.CullingOrderBy(context.Command.CommandText); SqlCommand sqlCount = $"SELECT COUNT(1) FROM ({cullingOrderBy}) TEMP"; return(sqlCount); }
internal override void BuildCommands(IProvider provider, DataTable table, DbConnection connection, DbTransaction transaction) { var syntax = provider.GetService <ISyntaxProvider>(); Initializate(provider, connection, transaction); var tableName = DbUtility.FormatByQuote(syntax, table.TableName); var sbSelect = new StringBuilder(string.Format("SELECT {{0}} FROM {0}", tableName)); var sbInsert = new StringBuilder(string.Format("INSERT INTO {0}({{0}}) VALUES({{1}})", tableName)); var sbUpdate = new StringBuilder(string.Format("UPDATE {0} SET {{0}} WHERE {{1}}", tableName)); var sbDelete = new StringBuilder(string.Format("DELETE FROM {0} WHERE {{0}}", tableName)); var sbFields = new StringBuilder(); var sbInsertValues = new StringBuilder(); var sbUpdateSets = new StringBuilder(); var sbWhere = new StringBuilder(); foreach (DataColumn column in table.Columns) { var columnName = DbUtility.FormatByQuote(syntax, column.ColumnName); if (sbFields.Length > 0) { sbFields.Append(", "); sbInsertValues.Append(", "); sbUpdateSets.Append(", "); sbWhere.Append(" AND "); } sbFields.Append(columnName); sbInsertValues.AppendFormat("{0}p{1}", syntax.ParameterPrefix, column.Ordinal + 1); sbUpdateSets.AppendFormat("{0} = {1}p{2}", columnName, syntax.ParameterPrefix, column.Ordinal + 1); sbWhere.AppendFormat("{0} = {1}p{2}", columnName, syntax.ParameterPrefix, column.Ordinal + 1); InsertCommand.Parameters.Add(CreateCommandParameter(provider, "p", column)); UpdateCommand.Parameters.Add(CreateCommandParameter(provider, "p", column)); DeleteCommand.Parameters.Add(CreateCommandParameter(provider, "p", column)); } SelectCommand.CommandText = string.Format(sbSelect.ToString(), sbFields); InsertCommand.CommandText = string.Format(sbInsert.ToString(), sbFields, sbInsertValues); UpdateCommand.CommandText = string.Format(sbUpdate.ToString(), sbUpdateSets, sbWhere); DeleteCommand.CommandText = string.Format(sbDelete.ToString(), sbWhere); }
private int GetRecordCountFromDatabase(CommandContext context) { var count = 0; var orderBy = DbUtility.FindOrderBy(context.Command.CommandText); var sql = string.IsNullOrEmpty(orderBy) ? context.Command.CommandText : context.Command.CommandText.Replace(orderBy, string.Empty); sql = string.Format("SELECT COUNT(*) FROM ({0}) TEMP", sql); using (var connection = context.Database.CreateConnection(DistributedMode.Slave)) { connection.OpenClose(() => { using (var command = context.Database.Provider.CreateCommand(connection, null, sql, parameters: context.Parameters)) { using (var reader = command.ExecuteReader()) { if (reader.Read()) { switch (reader.GetFieldType(0).GetDbType()) { case DbType.Decimal: count = (int)reader.GetDecimal(0); break; case DbType.Int32: count = reader.GetInt32(0); break; case DbType.Int64: count = (int)reader.GetInt64(0); break; } } } } }); } return(count); }
private int GetRecordCountFromDatabase(CommandContext context) { var count = 0; var cullingOrderBy = DbUtility.CullingOrderBy(context.Command.CommandText); var sqlCount = $"SELECT COUNT(1) FROM ({cullingOrderBy}) TEMP"; using (var connection = context.Database.CreateConnection(DistributedMode.Slave)) { connection.OpenClose(() => { using (var command = context.Database.Provider.CreateCommand(connection, null, sqlCount, parameters: context.Parameters)) { using (var reader = command.ExecuteReader()) { if (reader.Read()) { switch (reader.GetFieldType(0).GetDbType()) { case DbType.Decimal: count = (int)reader.GetDecimal(0); break; case DbType.Int32: count = reader.GetInt32(0); break; case DbType.Int64: count = (int)reader.GetInt64(0); break; } } } } }); } return(count); }