Exemplo n.º 1
0
        internal IEnumerable <IFSEntry> GetItemsEnumerator()
        {
            string query = "SELECT i.data FROM items i";

            System.Data.IDbCommand itemC = indexDbConn.CreateCommand();
            itemC.CommandText = query;
            System.Data.IDataReader reader = itemC.ExecuteReader();

            while (reader.Read())
            {
                int dataSize = (int)reader.GetBytes(0, 0, null, 0, 0);
                if (dataSize == 0)
                {
                    continue;
                }
                int    offset = 0, bytesRead = 0;
                byte[] buffer = new byte[dataSize];
                while (bytesRead < dataSize)
                {
                    bytesRead += (int)reader.GetBytes(0, offset, buffer, offset, (int)dataSize);
                    offset    += bytesRead;
                    Console.WriteLine("GetItemsEnumerator() : loop read=" + bytesRead);
                }
                using (dataMs = new MemoryStream()){
                    dataMs.Write(buffer, 0, bytesRead);
                    //dataMs.Flush();
                    dataMs.Position = 0;
                    using (gz = new System.IO.Compression.GZipStream(dataMs, System.IO.Compression.CompressionMode.Decompress, true)){
                        IFSEntry item = (IFSEntry)dataFormatter.Deserialize(gz);
                        yield return(item);
                    }
                }
            }
            dataMs.Close();
        }
Exemplo n.º 2
0
 public static byte[] Bytes(System.Data.IDataReader reader, MapPoint mapPoint, byte[] defaultValue = null)
 {
     try
     {
         if (mapPoint.IsInResultSet() && !reader.IsDBNull(mapPoint.Index))
         {
             if (mapPoint.Type == System.Data.SqlDbType.Binary || mapPoint.Type == System.Data.SqlDbType.Image)
             {
                 long   size       = reader.GetBytes(mapPoint.Index, 0, null, 0, 0);
                 byte[] values     = new byte[size];
                 int    bufferSize = 1024;
                 long   bytesRead  = 0;
                 int    curPos     = 0;
                 while (bytesRead < size)
                 {
                     bytesRead += reader.GetBytes(mapPoint.Index, curPos, values, curPos, bufferSize);
                     curPos    += bufferSize;
                 }
                 return(values);
             }
         }
     }
     catch (Exception)
     {
         throw;
     }
     return(defaultValue);
 }
Exemplo n.º 3
0
        internal void Open(long taskId)
        {
            this.Name     = "t" + taskId + ".idx";
            this.FullName = Path.Combine(Utilities.ConfigManager.GetValue("Backups.IndexFolder"), this.Name);
            if (!File.Exists(FullName))
            {
                throw new Exception("This index doesn't exists or doesn't have a local copy");
            }
            indexDbConn = new SqliteConnection();
            //indexDbConn.ConnectionString = "Version=3;Synchronous=off;Compress=True;data source=file:"+this.FullName;
            indexDbConn.ConnectionString = "Version=3,Synchronous=off,data source=file:" + this.FullName + "";
            indexDbConn.Open();
            dataFormatter = new BinaryFormatter();
            dataMs        = new MemoryStream();

            //get Header
            string headerQ = "SELECT data FROM header";

            System.Data.IDbCommand headerC = indexDbConn.CreateCommand();
            headerC.CommandText = headerQ;
            System.Data.IDataReader hReader = headerC.ExecuteReader();
            hReader.Read();
            dataMs = new MemoryStream();
            long dataSize = hReader.GetBytes(0, 0, null, 0, 0);
            int  offset = 0, bytesRead = 0;

            byte[] buffer = new byte[dataSize];
            //Console.WriteLine ("open() read="+hReader.GetBytes(0, 0, buffer, offset, BUFFER_SIZE));

            /*while((bytesRead = (int)hReader.GetBytes(0, offset, buffer, 0, 100)) > 0) {
             *      Console.WriteLine("open() read header : read="+bytesRead+", offset="+offset);
             *  dataMs.Write(buffer, 0, bytesRead);
             *  offset += bytesRead;
             *
             * }*/
            int curPos = 0;

            while (bytesRead < dataSize)
            {
                bytesRead += (int)hReader.GetBytes(0, curPos, buffer, curPos, (int)dataSize);
                curPos    += bytesRead;
            }
            dataMs.Write(buffer, 0, bytesRead);
            dataMs.Flush();
            //MemoryStream gzMs = new MemoryStream();
            dataMs.Position = 0;
            using (MemoryStream uncompressedStream = new MemoryStream()){
                using (gz = new System.IO.Compression.GZipStream(dataMs, System.IO.Compression.CompressionMode.Decompress, true)){
                    gz.CopyTo(uncompressedStream);
                }
                uncompressedStream.Position = 0;
                this.Header = (IndexHeader)dataFormatter.Deserialize(uncompressedStream);
            }

            dataMs.SetLength(0);
        }
Exemplo n.º 4
0
            /// <summary>
            /// 从数据读取器中指定字段读取相应的字节内容
            /// </summary>
            /// <param name="reader">数据读取器</param>
            /// <param name="column">指定字段</param>
            /// <param name="byteData">字节内容</param>
            /// <returns>short</returns>
            public static bool GetBytes(System.Data.IDataReader reader, int column, ref byte[] byteData)
            {
                MemoryStream memoryStream = null;
                BinaryWriter binaryWriter = null;

                try
                {
                    if (reader.IsDBNull(column))
                    {
                        byteData = new byte[0];
                        return(true);
                    }

                    memoryStream = new MemoryStream();
                    binaryWriter = new BinaryWriter(memoryStream);

                    int    nStartIndex = 0;
                    int    nBufferSize = 2048;
                    byte[] byteBuffer  = new byte[nBufferSize];
                    long   nRetLen     = reader.GetBytes(column, 0, byteBuffer, 0, nBufferSize);
                    while (nRetLen == nBufferSize)
                    {
                        binaryWriter.Write(byteBuffer);
                        binaryWriter.Flush();

                        nStartIndex += nBufferSize;
                        nRetLen      = reader.GetBytes(column, nStartIndex, byteBuffer, 0, nBufferSize);
                    }
                    binaryWriter.Write(byteBuffer, 0, (int)nRetLen);
                    binaryWriter.Flush();
                    byteBuffer = null;

                    byteData = memoryStream.ToArray();
                    return(true);
                }
                catch
                {
                    return(false);
                }
                finally
                {
                    if (binaryWriter != null)
                    {
                        binaryWriter.Close();
                    }
                    if (memoryStream != null)
                    {
                        memoryStream.Close();
                        memoryStream.Dispose();
                    }
                }
            }
Exemplo n.º 5
0
        } // End Sub RetrieveFile

        // http://stackoverflow.com/questions/2885335/clr-sql-assembly-get-the-bytestream
        // http://stackoverflow.com/questions/891617/how-to-read-a-image-by-idatareader
        // http://stackoverflow.com/questions/4103406/extracting-a-net-assembly-from-sql-server-2005
        public static void RetrieveFile(System.Data.IDbCommand cmd, string columnName, string path)
        {
            using (System.Data.IDataReader reader = ExecuteReader(cmd, System.Data.CommandBehavior.SequentialAccess | System.Data.CommandBehavior.CloseConnection))
            {
                bool hasRows = reader.Read();
                if (hasRows)
                {
                    const int BUFFER_SIZE = 1024 * 1024 * 10; // 10 MB
                    byte[]    buffer      = new byte[BUFFER_SIZE];

                    int col       = string.IsNullOrEmpty(columnName) ? 0 : reader.GetOrdinal(columnName);
                    int bytesRead = 0;
                    int offset    = 0;

                    // Write the byte stream out to disk
                    using (System.IO.FileStream bytestream = new System.IO.FileStream(path, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.None))
                    {
                        while ((bytesRead = (int)reader.GetBytes(col, offset, buffer, 0, BUFFER_SIZE)) > 0)
                        {
                            bytestream.Write(buffer, 0, bytesRead);
                            offset += bytesRead;
                        } // Whend

                        bytestream.Close();
                    } // End Using bytestream
                }     // End if (!hasRows)

                reader.Close();
            } // End Using reader
        }     // End Function RetrieveFile
Exemplo n.º 6
0
        }     // End Sub SimpleInsert

        // http://stackoverflow.com/questions/2885335/clr-sql-assembly-get-the-bytestream
        // http://stackoverflow.com/questions/891617/how-to-read-a-image-by-idatareader
        // http://stackoverflow.com/questions/4103406/extracting-a-net-assembly-from-sql-server-2005
        public static void RetrieveFile(string fileName, string path)
        {
            string sql = @"
--DECLARE @__filename nvarchar(255) 
--SET @__filename = 'lkik' 

SELECT 
	 uid
	,data
	,filename
FROM _____save
WHERE filename = @__filename 
";


            using (System.Data.IDbCommand cmd = SQL.CreateCommand(sql, 0))
            {
                SQL.AddParameter(cmd, "__filename", fileName);

                using (System.Data.IDataReader reader = SQL.ExecuteReader(cmd))
                {
                    bool hasRows = reader.Read();
                    if (hasRows)
                    {
                        const int BUFFER_SIZE = 1024 * 1024 * 10; // 10 megs
                        byte[]    buffer      = new byte[BUFFER_SIZE];

                        int col       = reader.GetOrdinal("data");
                        int bytesRead = 0;
                        int offset    = 0;

                        // write the byte stream out to disk
                        //using (System.IO.FileStream bytestream = new System.IO.FileStream(path, System.IO.FileMode.CreateNew))
                        using (System.IO.FileStream bytestream = new System.IO.FileStream(path, System.IO.FileMode.Create))
                        {
                            // SqlBytes bytes = reader.GetSqlBytes(0);
                            while ((bytesRead = (int)reader.GetBytes(col, offset, buffer, 0, BUFFER_SIZE)) > 0)
                            {
                                bytestream.Write(buffer, 0, bytesRead);
                                offset += bytesRead;
                            } // Whend

                            bytestream.Close();
                        } // End Using bytestream
                    }     // End if (!hasRows)

                    reader.Close();
                } // End Using reader
            }     // End Using cmd
        }         // End Function RetrieveFile
Exemplo n.º 7
0
        internal static MessageAttachment getAttachmentFromReader(System.Data.IDataReader rdr, Dictionary <string, bool> columnTable)
        {
            MessageAttachment attachment = new MessageAttachment();

            if (columnTable["ATTACHMENT_ID"])
            {
                int idIndex = rdr.GetOrdinal("ATTACHMENT_ID");
                if (!rdr.IsDBNull(idIndex))
                {
                    attachment.Id = Convert.ToInt32(rdr.GetDecimal(idIndex));
                }
            }
            if (columnTable["ATTACHMENT_NAME"])
            {
                int nameIndex = rdr.GetOrdinal("ATTACHMENT_NAME");
                if (!rdr.IsDBNull(nameIndex))
                {
                    attachment.AttachmentName = rdr.GetString(nameIndex);
                }
            }
            if (columnTable["ATTACHMENT"])
            {
                int attIndex = rdr.GetOrdinal("ATTACHMENT");
                if (!rdr.IsDBNull(attIndex))
                {
                    // not crazy about this implementation as it appears to invoke the reader twice but the commented out code
                    // block directly below throws an exception when calling GetOracleBlob for some reason... The good thing about
                    // this solution is it should work for all IDataReader implementations and doesn't need to be cast to an OracleDataReader
                    byte[] blob = new byte[rdr.GetBytes(attIndex, 0, null, 0, Int32.MaxValue)];
                    rdr.GetBytes(attIndex, 0, blob, 0, blob.Length);
                    attachment.SmFile = blob;
                    //if (rdr is Oracle.DataAccess.Client.OracleDataReader)
                    //{
                    //    System.Console.WriteLine(rdr[attIndex].GetType().ToString());
                    //    Oracle.DataAccess.Types.OracleBlob blob = ((Oracle.DataAccess.Client.OracleDataReader)rdr).GetOracleBlob(attIndex);
                    //    byte[] buf = new byte[blob.Length];
                    //    blob.Read(buf, 0, Convert.ToInt32(blob.Length));
                    //    attachment.SmFile = buf;
                    //}
                }
            }
            if (columnTable["MIME_TYPE"])
            {
                int mimeTypeIndex = rdr.GetOrdinal("MIME_TYPE");
                if (!rdr.IsDBNull(mimeTypeIndex))
                {
                    attachment.MimeType = rdr.GetString(mimeTypeIndex);
                }
            }
            if (columnTable["ATTOPLOCK"])
            {
                int oplockIndex = rdr.GetOrdinal("ATTOPLOCK");
                if (!rdr.IsDBNull(oplockIndex))
                {
                    attachment.Oplock = Convert.ToInt32(rdr.GetDecimal(oplockIndex));
                }
            }


            return(attachment);
        }
Exemplo n.º 8
0
        public static void SaveAssembly(string assemblyName, string path)
        {
            string sql = @"
--DECLARE @__assemblyname nvarchar(260)
--SET @__assemblyname = 'Microsoft.SqlServer.Types'
SELECT 
	 A.name
	,AF.content 
FROM sys.assembly_files AS AF 
INNER JOIN sys.assemblies AS A 
	ON AF.assembly_id = A.assembly_id 
	
WHERE AF.file_id = 1 
AND A.name = @__assemblyname
;
";


            AnySqlWebAdmin.SqlService service = new AnySqlWebAdmin.SqlService();


            using (System.Data.Common.DbConnection con = service.Connection)
            {
                using (System.Data.Common.DbCommand cmd = con.CreateCommand())
                {
                    cmd.CommandText = sql;


                    var p = cmd.CreateParameter();
                    p.ParameterName = "__assemblyname";
                    p.DbType        = System.Data.DbType.String;
                    p.Value         = assemblyName;

                    cmd.Parameters.Add(p);


                    if (con.State != System.Data.ConnectionState.Open)
                    {
                        con.Open();
                    }

                    using (System.Data.IDataReader reader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection))
                    {
                        reader.Read();
                        //SqlBytes bytes = reader.GetSqlBytes(0);
                        const int BUFFER_SIZE = 1024;
                        byte[]    buffer      = new byte[BUFFER_SIZE];

                        int col       = reader.GetOrdinal("content");
                        int bytesRead = 0;
                        int offset    = 0;

                        // write the byte stream out to disk
                        using (System.IO.FileStream bytestream = new System.IO.FileStream(path, System.IO.FileMode.CreateNew))
                        {
                            while ((bytesRead = (int)reader.GetBytes(col, offset, buffer, 0, BUFFER_SIZE)) > 0)
                            {
                                bytestream.Write(buffer, 0, bytesRead);
                                offset += bytesRead;
                            } // Whend

                            bytestream.Close();
                        } // End Using bytestream

                        reader.Close();
                    } // End Using reader

                    if (con.State != System.Data.ConnectionState.Closed)
                    {
                        con.Close();
                    }
                }
            }
        } // End Function SaveAssembly
Exemplo n.º 9
0
        }     // End Sub SaveAssembly2

        // http://stackoverflow.com/questions/2885335/clr-sql-assembly-get-the-bytestream
        // http://stackoverflow.com/questions/891617/how-to-read-a-image-by-idatareader
        // http://stackoverflow.com/questions/4103406/extracting-a-net-assembly-from-sql-server-2005
        public virtual void SaveAssembly3(string assemblyName, string path)
        {
            string sql = @"
--DECLARE @__assemblyname nvarchar(260)
--SET @__assemblyname = 'Microsoft.SqlServer.Types'


SELECT 
	 A.name
	,AF.content 
FROM sys.assembly_files AS AF 

INNER JOIN sys.assemblies AS A 
	ON AF.assembly_id = A.assembly_id 
	
WHERE AF.file_id = 1 
AND A.name = @__assemblyname
;

";

            using (System.Data.IDbConnection conn = new System.Data.SqlClient.SqlConnection("context connection=true"))   //Create current context connection
            {
                using (System.Data.IDbCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;

                    System.Data.IDbDataParameter param = cmd.CreateParameter();
                    param.ParameterName = "@__assemblyname";
                    param.DbType        = System.Data.DbType.String;
                    param.Value         = assemblyName;
                    param.Size          = 128;
                    cmd.Parameters.Add(param);
                    cmd.Prepare();


                    using (System.Data.IDataReader reader = cmd.ExecuteReader())
                    {
                        reader.Read();

                        const int BUFFER_SIZE = 1024;
                        byte[]    buffer      = new byte[BUFFER_SIZE];

                        int col       = reader.GetOrdinal("content");
                        int bytesRead = 0;
                        int offset    = 0;

                        // write the byte stream out to disk
                        using (System.IO.FileStream bytestream = new System.IO.FileStream(path, System.IO.FileMode.CreateNew))
                        {
                            while ((bytesRead = (int)reader.GetBytes(col, offset, buffer, 0, BUFFER_SIZE)) > 0)
                            {
                                bytestream.Write(buffer, 0, bytesRead);
                                offset += bytesRead;
                            } // Whend

                            bytestream.Close();
                        } // End Using bytestream

                        reader.Close();
                    } // End Using reader
                }     // End Using cmd
            }         // End Using conn
        }             // End Function SaveAssembly3
Exemplo n.º 10
0
        public void SetFromReader <T>(T obj, System.Data.IDataReader reader, Action <T, string, object> setMethod)
            where T : class
        {
            if (!reader.IsDBNull(this.Index))
            {
                switch (this.SqlType)
                {
                case System.Data.SqlDbType.BigInt:
                    setMethod(obj, FieldName, reader.GetInt64(this.Index));
                    break;

                case System.Data.SqlDbType.Image:
                case System.Data.SqlDbType.VarBinary:
                case System.Data.SqlDbType.Binary:
                    long   size       = reader.GetBytes(this.Index, 0, null, 0, 0);
                    byte[] values     = new byte[size];
                    int    bufferSize = 1024;
                    long   bytesRead  = 0;
                    int    curPos     = 0;
                    while (bytesRead < size)
                    {
                        bytesRead += reader.GetBytes(this.Index, curPos, values, curPos, bufferSize);
                        curPos    += bufferSize;
                    }
                    setMethod(obj, FieldName, values);
                    break;

                case System.Data.SqlDbType.Bit:
                    setMethod(obj, FieldName, reader.GetBoolean(this.Index));
                    break;

                case System.Data.SqlDbType.Char:
                    setMethod(obj, FieldName, reader.GetChar(this.Index));
                    break;

                case System.Data.SqlDbType.SmallDateTime:
                case System.Data.SqlDbType.Date:
                case System.Data.SqlDbType.DateTime:
                case System.Data.SqlDbType.DateTime2:
                    setMethod(obj, FieldName, reader.GetDateTime(this.Index));
                    break;

                case System.Data.SqlDbType.SmallMoney:
                case System.Data.SqlDbType.Money:
                case System.Data.SqlDbType.Decimal:
                    setMethod(obj, FieldName, reader.GetDecimal(this.Index));
                    break;

                case System.Data.SqlDbType.Float:
                    setMethod(obj, FieldName, reader.GetDouble(this.Index));
                    break;

                case System.Data.SqlDbType.Int:
                    setMethod(obj, FieldName, reader.GetInt32(this.Index));
                    break;

                case System.Data.SqlDbType.Text:
                case System.Data.SqlDbType.NVarChar:
                case System.Data.SqlDbType.NText:
                case System.Data.SqlDbType.VarChar:
                case System.Data.SqlDbType.NChar:
                    setMethod(obj, FieldName, reader.GetString(this.Index));
                    break;

                case System.Data.SqlDbType.Real:
                    setMethod(obj, FieldName, reader.GetFloat(this.Index));
                    break;

                case System.Data.SqlDbType.SmallInt:
                    setMethod(obj, FieldName, reader.GetInt16(this.Index));
                    break;

                case System.Data.SqlDbType.TinyInt:
                    setMethod(obj, FieldName, reader.GetByte(this.Index));
                    break;

                case System.Data.SqlDbType.UniqueIdentifier:
                    setMethod(obj, FieldName, reader.GetGuid(this.Index));
                    break;

                default:
                    break;
                }
            }
            else
            {
                setMethod(obj, FieldName, null);
            }
        }
Exemplo n.º 11
0
 long System.Data.IDataRecord.GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length)
 {
     return(_dataReader.GetBytes(i, fieldOffset, buffer, bufferoffset, length));
 }