Comments (3)
Any news on this? it would be a great feature
from dbextensions.
You can override Database.CreateCommand method to implement it.
from dbextensions.
Just encase anyone wants help on this, this is what i have implemented by overriding the Database.CreateCommand
method
So it checks if the command has over 2050 parameters (using constant) if under then proceeds as normal.
If over, then it will create unique parameters for each value, put them in a dictionary and use those instead. If by some reason that goes over the declared 2050 parameters, then it will resort to using values (which i currently have commented out in our code)
public class DataContextCommandDatabase : Database
{
private const int SQL_MAX_PARAMETER_COUNT = 2050;
public DataContextCommandDatabase(IDbConnection connection) : base(connection) { }
public new IDbCommand CreateCommand(string commandText, params object[] parameters)
{
if (parameters == null || parameters.Length < SQL_MAX_PARAMETER_COUNT)
{
return base.CreateCommand(commandText, parameters);
}
else
{
// this code is taken directly from Database base class
if (commandText == null) throw new ArgumentNullException(nameof(commandText));
IDbCommand command = this.Connection.CreateCommand();
IDbTransaction transaction = this.Transaction;
if (transaction != null)
{
command.Transaction = transaction;
}
int commandTimeout = this.Configuration.CommandTimeout;
if (commandTimeout > -1)
{
command.CommandTimeout = commandTimeout;
}
var paramIndex = 0;
var commandParameters = new Dictionary<object, IDataParameter>();
var paramPlaceholders = new object[parameters.Length];
// i have just added checks here to see what it is, atm i am always using parameters on strings
for (int i = 0; i < paramPlaceholders.Length; i++)
{
object paramValue = parameters[i];
if (commandParameters.ContainsKey(paramValue))
{
// parameter with value already exists, so use it
var dbParam = commandParameters[paramValue];
paramPlaceholders[i] = this.Configuration.ParameterPlaceholderBuilder(dbParam.ParameterName);
}
else if (commandParameters.Keys.Count < SQL_MAX_PARAMETER_COUNT)
{
IDataParameter dbParam = paramValue as IDataParameter;
if (dbParam == null)
{
dbParam = command.CreateParameter();
dbParam.Value = paramValue ?? DBNull.Value;
}
dbParam.ParameterName = this.Configuration.ParameterNameBuilder("p" + paramIndex.ToString(CultureInfo.InvariantCulture));
command.Parameters.Add(dbParam);
paramPlaceholders[i] = this.Configuration.ParameterPlaceholderBuilder(dbParam.ParameterName);
commandParameters[paramValue] = dbParam;
paramIndex++;
}
else if (paramValue == null)
{
paramPlaceholders[i] = "NULL";
}
else
{
if (paramValue is bool)
{
paramPlaceholders[i] = ((bool)paramValue) ? 1 : 0;
}
else if (paramValue is DateTime)
{
var asDate = (DateTime)paramValue;
paramPlaceholders[i] = "CAST('" + asDate.ToString("yyyy-MM-dd HH:mm:ss.fff") + "' AS DATETIME2(0))";
}
else if (paramValue is string)
{
paramPlaceholders[i] = "'" + paramValue.ToString().Replace("'", "''") + "'";
}
else
{
paramPlaceholders[i] = paramValue;
}
}
}
command.CommandText = string.Format(CultureInfo.InvariantCulture, commandText, paramPlaceholders);
return command;
}
}
}
from dbextensions.
Related Issues (20)
- Need to "extend" clause HOT 1
- dotnet core version HOT 23
- Support explicit DbParameter on CreateCommand HOT 3
- Add support for CROSS_JOIN to SqlBuilder HOT 2
- Async support HOT 7
- CommandTimeout setting HOT 4
- Accessing CommandTimeout property HOT 1
- Support updates when using updatable keys HOT 1
- LIMIT and OFFSET incorrectly adding {0} instead of actual value. HOT 1
- Please create a DotNet Standard Version. HOT 1
- Compile with C# 8 to leverage nullable reference types annotations HOT 1
- Association with multiple keys HOT 4
- Error using SqlSet using depth path HOT 2
- Add support for .NetStandard 2.1 HOT 1
- Use same parameter when value is the same HOT 6
- ColumnAttribute Name does not work HOT 8
- Expose table name metadata
- .NET 6 support? HOT 2
- [self-resolved] ._ForEach<T>() behaves differently depending on .NET environment ?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dbextensions.