当前位置: 首页>>代码示例>>C#>>正文


C# DbCommand.GetType方法代码示例

本文整理汇总了C#中DbCommand.GetType方法的典型用法代码示例。如果您正苦于以下问题:C# DbCommand.GetType方法的具体用法?C# DbCommand.GetType怎么用?C# DbCommand.GetType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在DbCommand的用法示例。


在下文中一共展示了DbCommand.GetType方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetLastInsertedId

        /// <summary>
        /// Gets the ID for the row that was inserted into the database. Only valid when the
        /// query contains an auto-increment column and the operation being performed is an insert.
        /// </summary>
        /// <param name="command">The <see cref="DbCommand"/> that was executed and the last inserted id needs to be acquired for.</param>
        /// <returns>
        /// The last inserted id for the executed <paramref name="command"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException"><paramref name="command"/> is null.</exception>
        /// <exception cref="TypeException"><paramref name="command"/> is not of the excepted type.</exception>
        /// <exception cref="ArgumentException"><paramref name="command"/> is invalid in some other way.</exception>
        public override long GetLastInsertedId(DbCommand command)
        {
            if (command == null)
                throw new ArgumentNullException("command");

            var c = command as MySqlCommand;
            if (c == null)
            {
                const string errmsg = "Expected DbCommand `{0}` to be of type `{1}`, but was type `{2}`.";
                throw new TypeException(string.Format(errmsg, command, typeof(MySqlCommand), command.GetType()));
            }

            Debug.Assert(c.LastInsertedId != 0,
                "The LastInsertedId shouldn't ever be 0 for MySql since AutoIncrement starts at 1...");

            return c.LastInsertedId;
        }
开发者ID:mateuscezar,项目名称:netgore,代码行数:28,代码来源:MySqlDbConnectionPool.cs

示例2: AddParameterWithName

        protected void AddParameterWithName (DbCommand command, string parameterName, object value)
        {
            if (command.Parameters.GetType().Name == "SqlParameterCollection")
            {
                // The SqlParameterCollection only accepts SqlParameter objects, can't add value types directly

                SqlDbType parameterType = SqlDbType.NText;

                if (value is Type)
                {
                    if ((Type) value == typeof(int))
                    {
                        parameterType = SqlDbType.Int;
                    }
                    else if ((Type)value == typeof(DateTime))
                    {
                        parameterType = SqlDbType.DateTime;
                    }
                    else if ((Type)value == typeof(bool))
                    {
                        parameterType = SqlDbType.Bit;
                    }
                    else if ((Type)value == typeof(double) || (Type)value == typeof(float))
                    {
                        parameterType = SqlDbType.Float;
                    }
                    else if ((Type)value == typeof(decimal))
                    {
                        parameterType = SqlDbType.Money;
                    }
                    else if (!((Type)value == typeof(string)))
                    {
                        throw new Exception("Unhandled parameter type in AddParameterWithName: " + value.GetType().Name);
                    }
                    value = DBNull.Value;
                }
                else if (value is int)
                {
                    parameterType = SqlDbType.Int;
                }
                else if (value is DateTime)
                {
                    parameterType = SqlDbType.DateTime;
                }
                else if (value is bool)
                {
                    parameterType = SqlDbType.Bit;
                }
                else if (value is double || value is float)
                {
                    parameterType = SqlDbType.Float;
                }
                else if (value is decimal)
                {
                    parameterType = SqlDbType.Money;
                }
                else if (!(value is string))
                {
                    throw new Exception("Unhandled parameter type in AddParameterWithName: " + value.GetType().Name);
                }

                System.Data.SqlClient.SqlParameter parameter = null;

                if (parameterType == SqlDbType.NText)
                {
                    int textLength = 256*2;
                    if (value != DBNull.Value)
                    {
                        textLength = (value as string).Length * 2; // ntext / unicode buffer

                        // establish a minimum amount of even 256-byte blocks that this string will fit in

                        textLength += 2; // terminating zero, in unicode
                        textLength -= (textLength % 256);
                        textLength += 256;
                    }
                    parameter = new System.Data.SqlClient.SqlParameter("@" + parameterName, parameterType, textLength);
                }
                else
                {
                    // For all other types, ignore the length parameter, let the default handle it

                    parameter = new System.Data.SqlClient.SqlParameter("@" + parameterName, parameterType);
                }

                parameter.Value = value;

                System.Data.SqlClient.SqlParameterCollection sqlCollection =
                    command.Parameters as System.Data.SqlClient.SqlParameterCollection;

                sqlCollection.Add(parameter);
            }
            else if (command.GetType().FullName == "MySql.Data.MySqlClient.MySqlCommand")
            {
                MySqlParameter newParameter = new MySqlParameter(parameterName, value);
                command.Parameters.Add(newParameter);
            }
            else
            {
                int i = command.Parameters.Add(value);
//.........这里部分代码省略.........
开发者ID:SwarmCorp,项目名称:Swarmops,代码行数:101,代码来源:Database-ConnectionsCommands.cs


注:本文中的DbCommand.GetType方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。