GetDataTypeName() public méthode

Retrieve the SQL data type name of a specified column (e.g. "varchar", "int", "smallint", et al.).
public GetDataTypeName ( int columnIndex ) : string
columnIndex int Number of the column.
Résultat string
Exemple #1
0
        /// <summary>
        /// Initialize the DatabaseRow with a single row form a Halide.H3Reader object.
        /// </summary>
        /// <param name="reader">An open Halide.H3Reader object that can be used to read a row from a database.</param>
        public DatabaseRow(H3Reader reader)
        {
            _fieldCount = reader.FieldCount;

            try
            {
                _dataItems = new DatabaseItem[_fieldCount];

                if (_dataIndex != null) _dataIndex.Clear();

                for (int x = 0; x < _fieldCount; x++)
                {
                    _dataItems[x] = new DatabaseItem();

                    String columnName = reader.ColumnName(x).ToLower();

                    _dataIndex.Add(columnName, x);

                    _dataItems[x].IsNullable = reader.GetColumnAllowNulls(columnName);
                    _dataItems[x].ColumnName = columnName;
                    _dataItems[x].ColumnSize = reader.GetColumnSize(columnName);
                    _dataItems[x].ColumnSQLType = reader.GetDataTypeName(columnName).ToLower();
                    _dataItems[x].IsAutoIncrementing = reader.GetColumnIsAutoIncrement(columnName);
                    _dataItems[x].IsIdentity = reader.GetColumnIsIdentity(columnName);
                    _dataItems[x].IsPrimaryKey = reader.GetColumnIsKey(columnName);
                    _dataItems[x].SystemDataType = reader.GetColumnSystemDataType(columnName).ToLower();
                    _dataItems[x].Value = reader.GetString(columnName);
                }
            }
            catch
            {
            }
        }
Exemple #2
0
        /// <summary>
        /// Generate CSV data from a SQL Server request. Converts quotation marks to "&quot;".
        /// </summary>
        /// <param name="select">SQL Server command to execute, which retrieves a dataset.</param>
        /// <param name="connectionStringName">Connection String name in the Web.config file.</param>
        /// <param name="includeHeaders">Include column names as a header row in the CSV file.</param>
        /// <param name="alwaysUseQuotes">Wrap all values in quotation marks. False will omit quotation marks around numeric values.</param>
        /// <returns>StringBuilder result with the new CSV data.</returns>
        public static StringBuilder GenerateCSV(String select, String connectionStringName, Boolean includeHeaders, Boolean alwaysUseQuotes)
        {
            StringBuilder csv = new StringBuilder();

            using (H3Reader reader = new H3Reader(select, true, connectionStringName))
            {
                #region Build header row

                if (includeHeaders)
                {
                    for (Int32 x = 0; x < reader.FieldCount; x++)
                    {
                        if (x > 0)
                        {
                            csv.Append(",");
                        }

                        csv.Append("\"" + reader.ColumnName(x) + "\"");
                    }

                    csv.Append("\r\n");
                }

                #endregion

                #region Build data rows

                ArrayList types = new ArrayList();
                types.Add("int");
                types.Add("bigint");
                types.Add("float");
                types.Add("decimal");
                types.Add("numeric");
                types.Add("money");
                types.Add("tinyint");
                types.Add("int");
                types.Add("smallint");
                types.Add("smallmoney");
                types.Add("real");

                while (reader.Read())
                {
                    for (Int32 x = 0; x < reader.FieldCount; x++)
                    {
                        if (x > 0)
                        {
                            csv.Append(",");
                        }

                        if (!alwaysUseQuotes && types.Contains(reader.GetDataTypeName(x).ToLower()))
                        {
                            csv.Append(reader[x]);
                        }

                        else
                        {
                            csv.Append("\"" + reader[x].Replace("\"", "&quot;") + "\"");
                        }
                    }

                    csv.Append("\r\n");
                }

                #endregion
            }

            return csv;
        }