Git Product home page Git Product logo

Comments (3)

Gillardo avatar Gillardo commented on July 3, 2024

Any news on this? it would be a great feature

from dbextensions.

maxtoroq avatar maxtoroq commented on July 3, 2024

You can override Database.CreateCommand method to implement it.

from dbextensions.

Gillardo avatar Gillardo commented on July 3, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.