Пример #1
0
        private static QueryParameters GetParametersFromCommandText(string commandText)
        {
            var p = new QueryParameters();
            var uniqueParameters = new HashSet <string>();

            foreach (Match match in Regex.Matches(commandText, @"@([^=<>\s\'\),-[\\+*/\n]]+)"))
            {
                var paramName = match.Groups[1].Value;

                if (!p.Contains(paramName))
                {
                    p.Add(new QueryParameter(paramName));
                }
            }
            return(p);
        }
Пример #2
0
        public static DbCommand GetCommand(this DbConnection con, string sql, QueryParameters parameters = null, DbTransaction transaction = null)
        {
            var cmd = con.CreateCommand();

            cmd.CommandText = sql;
            cmd.Transaction = transaction;

            //Contract has to be class or struct

            if (parameters != null)
            {
                //var i = 0;

                if (parameters.HasNamedParameters)
                {
                    foreach (var p in parameters)
                    {
                        cmd.AddParameter(p.Name, p.Value);
                    }
                }
                else
                {
                    var commandParameters = GetParametersFromCommandText(sql);
                    foreach (var cp in commandParameters)
                    {
                        if (parameters.Contains(cp.Name))
                        {
                            var p = parameters[cp.Name];
                            cmd.AddParameter(p.Name, p.Value);
                        }
                    }
                }
            }

            return(cmd);
        }