コード例 #1
0
 public int WriteRow(StorageDataReader dataReader)
 {
     throw new InvalidOperationException("This type of writer is meant to write values from a list of cell values only.");
 }
コード例 #2
0
        /// <summary>
        /// Writes an entire row to the file stream
        /// </summary>
        /// <param name="reader">A primed reader</param>
        /// <returns>Number of bytes used to write the row</returns>
        public int WriteRow(StorageDataReader reader)
        {
            // Read the values in from the db
            object[] values = new object[reader.Columns.Length];
            if (!reader.HasLongColumns)
            {
                // get all record values in one shot if there are no extra long fields
                reader.GetValues(values);
            }

            // Loop over all the columns and write the values to the temp file
            int rowBytes = 0;

            for (int i = 0; i < reader.Columns.Length; i++)
            {
                DbColumnWrapper ci = reader.Columns[i];
                if (reader.HasLongColumns)
                {
                    if (reader.IsDBNull(i))
                    {
                        // Need special case for DBNull because
                        // reader.GetValue doesn't return DBNull in case of SqlXml and CLR type
                        values[i] = DBNull.Value;
                    }
                    else
                    {
                        if (!ci.IsLong)
                        {
                            // not a long field
                            values[i] = reader.GetValue(i);
                        }
                        else
                        {
                            // this is a long field
                            if (ci.IsBytes)
                            {
                                values[i] = reader.GetBytesWithMaxCapacity(i, maxCharsToStore);
                            }
                            else if (ci.IsChars)
                            {
                                Debug.Assert(maxCharsToStore > 0);
                                values[i] = reader.GetCharsWithMaxCapacity(i,
                                                                           ci.IsXml ? maxXmlCharsToStore : maxCharsToStore);
                            }
                            else if (ci.IsXml)
                            {
                                Debug.Assert(maxXmlCharsToStore > 0);
                                values[i] = reader.GetXmlWithMaxCapacity(i, maxXmlCharsToStore);
                            }
                            else
                            {
                                // we should never get here
                                Debug.Assert(false);
                            }
                        }
                    }
                }

                // Get true type of the object
                Type tVal = values[i].GetType();

                // Write the object to a file
                if (tVal == typeof(DBNull))
                {
                    rowBytes += WriteNull();
                }
                else
                {
                    if (ci.IsSqlVariant)
                    {
                        // serialize type information as a string before the value
                        string val = tVal.ToString();
                        rowBytes += WriteString(val);
                    }

                    // Use the appropriate writing method for the type
                    Func <object, int> writeMethod;
                    if (writeMethods.TryGetValue(tVal, out writeMethod))
                    {
                        rowBytes += writeMethod(values[i]);
                    }
                    else
                    {
                        rowBytes += WriteString(values[i].ToString());
                    }
                }
            }

            // Flush the buffer after every row
            FlushBuffer();
            return(rowBytes);
        }