System.Data.Common.DbConnection.CreateCommand C# (CSharp) Method

CreateCommand() public method

public CreateCommand ( ) : DbCommand
return DbCommand
        public DbCommand CreateCommand() => CreateDbCommand();

Usage Example

        private static void CreateExtensionTable(DbConnection connection, ExtendedTable extendedTable)
        {
            using (var queryCommand = connection.CreateCommand())
            {
                string primaryKeyColumn = GetPrimaryKey(connection, extendedTable.BaseTableName);

                queryCommand.CommandText = string.Format(
                    "SELECT {0} FROM {1}",
                    primaryKeyColumn,
                    extendedTable.BaseTableName);

                string dataType = null;
                string properties =
                    string.Join(
                        ", ",
                        new [] { string.Format("{0} uniqueidentifier PRIMARY KEY", primaryKeyColumn) }
                        .Union(
                            from property in extendedTable.ExtendedProperties
                            where TypeMapping.TryGetValue(property.PropertyType, out dataType)
                            select string.Format("{0} {1}", property.PropertyName, dataType)));

                var creationCommand = connection.CreateCommand();
                creationCommand.CommandText = string.Format(
                    "CREATE TABLE {0} ({1});",
                    extendedTable.TableName,
                    properties);
                creationCommand.ExecuteNonQuery();

                using (DbDataReader reader = queryCommand.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        object keyValue = reader[primaryKeyColumn];

                        creationCommand = connection.CreateCommand();
                        creationCommand.CommandText = string.Format(
                            "INSERT INTO {0} ({1}) VALUES (@value);",
                            extendedTable.TableName,
                            primaryKeyColumn);

                        var parameter = creationCommand.CreateParameter();
                        parameter.DbType = DbType.Guid;
                        parameter.ParameterName = "@value";
                        parameter.Value = keyValue;

                        creationCommand.Parameters.Add(parameter);

                        creationCommand.ExecuteNonQuery();
                    }
                }
            }
        }
All Usage Examples Of System.Data.Common.DbConnection::CreateCommand