SQLite 작업

9335 단어 sqlite
            string  dbPath="Data Source=D:\\test.db3";
            using (SQLiteConnection cnn = new SQLiteConnection(dbPath))
            {
                cnn.Open();   // 

                using (SQLiteCommand cmd = cnn.CreateCommand())
                {
                    byte[] buffer = null;

                    // 
                    string sql = "create table test(id int,file blob);";
                    cmd.CommandText = sql;

                    // Blob 
                    string file = @"d:\help.jpg";
                    buffer = FileHelper.FileToBytebuffer(file);
                    cmd.CommandText = "insert into test values('11',@data)";
                    SQLiteParameter para = new SQLiteParameter("@data", DbType.Binary);
                    para.Value = buffer;
                    cmd.Parameters.Add(para);
                    cmd.ExecuteNonQuery();

                    // , Blob 
                    cmd.CommandText = "SELECT * FROM test";// WHERE ID = '12'";
                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            string f = reader["id"].ToString();  // 
                           buffer =Helpers.Get_SQLite_Blob_Bytes(reader,1); // 
                            FileHelper.BytebufferToFile(buffer, "d:\\"+f+".jpg");
                        }
                    }                   

                    // Blob 
                    Byte[] m_byte = FileHelper.FileToBytebuffer("d:\\pic1.jpg");
                    cmd.CommandText = "UPDATE test set file=@file WHERE id=12";
                    SQLiteParameter param_m = new SQLiteParameter("@file", DbType.Binary, m_byte.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, m_byte);
                    cmd.Parameters.Add(param_m); // , DBType.Binary 
                    cmd.ExecuteNonQuery();

                    // 
                    cmd.CommandText = "delete from test where id=12";
                    cmd.ExecuteNonQuery();

                    // ,
                    cmd.CommandText = @"create table if not exists tes (ID integer primary key autoincrement not null, tojid varchar(200),timestamp timestamp,remark,data text,type int)";
                    cmd.ExecuteNonQuery();
} }

 
            /// <summary>
            ///  SQLite , 
            /// </summary>
            /// <param name="reader">SQLiteDataReader</param>
            /// <param name="columnnumber"> </param>
            /// <returns>byte[]</returns>
            public static byte[] Get_SQLite_Blob_Bytes(SQLiteDataReader reader, int columnnumber)
            {
                const int CHUNK_SIZE = 2 * 1024;
                byte[] buffer = new byte[CHUNK_SIZE];
                long bytesRead;
                long fieldOffset = 0; using (MemoryStream stream = new MemoryStream())
                {
                    while ((bytesRead = reader.GetBytes(columnnumber, fieldOffset, buffer, 0, buffer.Length)) > 0)
                    {
                        stream.Write(buffer, 0, (int)bytesRead);
                        fieldOffset += bytesRead;
                    } return stream.ToArray();
                }
            }

좋은 웹페이지 즐겨찾기