Add BuildParameterPlaceholder(name) Method for database class

Topics: Data Access Application Block
Jul 30, 2009 at 9:21 AM

if it exist,i can use invariant command text, then transform it to special dbprovider

Jul 30, 2009 at 10:55 AM

I think the question is not clear to me. Can you explain it a bit more?

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Jul 31, 2009 at 1:30 AM

I execute a command with the parameters, different database is not the same as a parameter placeholder, such as: Sqlserver is' @ parametername ', Oracle is the': parametername 'and so on; if ‘database class’ has a method to obtain placeholder information on the parameter name,it can resolve the differences between the CommandText .
I hope not all of the command with the parameters must be stored procedure

My English is poor,  please forgive

Jul 31, 2009 at 3:00 AM

See if this answers your question: http://entlib.codeplex.com/Thread/View.aspx?ThreadId=62233

 

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com

Jul 31, 2009 at 3:30 AM

Add this simple method may resolve it;

follow as:

1、database class

        public virtual string BuildParameterPlaceholder(string name)
        {
            return "?";
        }

2、sqldatabase class

        public override string BuildParameterPlaceholder(string name)
        {
            if (name[0] != this.ParameterToken)
            {
                return name.Insert(0, new string(this.ParameterToken, 1));
            }
            return name;
        }

3、oralcedatabase class

        public override string BuildParameterPlaceholder(string name)
        {
            if (name[0] != ':')
            {
                return name;
            }
            return name.Substring(1);
        }
4、add interface
    public interface IParameterPlaceholderHandler
    {
        void Handle(Database db, DbCommand command);
    }
5、add a default implement
    public sealed class DefaultPlaceholderHandler : IParameterPlaceholderHandler
    {

        public void Handle(Database db, DbCommand command)
        {
            if (command.CommandType == CommandType.Text)
            {
                String[] items = command.CommandText.Split(new char[] { '?' }, StringSplitOptions.None);
                if (command.Parameters.Count != (items.Length - 1))
                {
                    throw new ArgumentException();
                }
                StringBuilder sb = new StringBuilder();
                sb.Append(items[0]);
                for (int i = 1; i < items.Length; i++)
                {
                    sb.Append(db.BuildParameterPlaceholder(command.Parameters[i - 1].ParameterName));
                    sb.Append(items[i]);
                }
            }
            else
                throw new NotSupportedException();
        }

    }
6、Use
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand command = db.GetSqlStringCommand("select name from t1 where id = ?");
            db.AddInParameter(command, "id", DbType.Int32, 100);
            new DefaultPlaceholderHandler().Handle(db, command);
            db.ExecuteReader(command);

this is that I want
Jul 31, 2009 at 4:45 AM

The current option you have is to modify the source code itself and just log this as a feature request.

 

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com

Jul 31, 2009 at 4:51 AM

I hope next version can include this feature