Beispiel #1
0
        /// <summary>
        /// Insert a ingle row
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="connection"></param>
        /// <param name="param">object</param>
        /// <param name="table">Optional table name</param>
        /// <param name="commandTimeout">commandTimeout</param>
        /// <param name="transaction">transaction</param>
        /// <returns>Numbers of rows affected</returns>
        public static int InsertSingle <T>(this IDbConnection connection, T param, int?commandTimeout = null, IDbTransaction transaction = null)
        {
            if (param == null)
            {
                throw new ArgumentNullException("param can not be null.");
            }

            if (param is IEnumerable)
            {
                throw new ArgumentException("param can not be a IEnumerable. Call InsertMany instead.");
            }

            var type = typeof(T);

            string cachedCommand;
            var    value = StringCache.TryGetCommand(type, Operation.Insert, out cachedCommand);

            if (string.IsNullOrEmpty(cachedCommand))
            {
                cachedCommand = InsertGenerator.GenerateSingle(param);
                StringCache.Add(type, Operation.Insert, cachedCommand);
            }

            return(connection.Execute(cachedCommand, param, commandTimeout: commandTimeout, transaction: transaction));
        }
        public void GenerateInsertCommand()
        {
            var command = InsertGenerator.GenerateSingle(new Bar
            {
                Foo1 = "uma string",
                Foo2 = "outra string",
                Foo3 = "mais uma string",
                Foo4 = true,
                Foo5 = null,
                Foo6 = 15.1m
            });

            Assert.AreEqual("INSERT INTO Bar (Foo1, Foo2, Foo3, Foo4, Foo5, Foo6, Foo7) VALUES (@Foo1, @Foo2, @Foo3, @Foo4, @Foo5, @Foo6, @Foo7);", command);
        }