Dictionary<>二进制存储

Name表字段设置为 varbinary(MAX)


存储:

SqlConnection connection = new SqlConnection("Initial Catalog=TestDB;User ID=sa;Password=sa;Data Source=.");



            Dictionary<string, bool> dk = new Dictionary<string, bool>();
            dk.Add("tsFileName",true);
            dk.Add("tsclose", false);




              // 转换操作员权限集合为数据库可存取的 Byte[] 数组
            MemoryStream ms = new MemoryStream();
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(ms, dk);
            byte[] rigthsByteArray = new byte[(int)(ms.Length)];
            ms.Position = 0;
            ms.Read(rigthsByteArray, 0, (int)(ms.Length));
            ms.Close();
            // 拼接 SQL 命令
            string sqlTxt = "insert into Test (Name) values (@OperatorName)";


            SqlCommand cmd = new SqlCommand(sqlTxt, connection);
                SqlParameter prm3 = new SqlParameter("@OperatorName", SqlDbType.VarBinary, rigthsByteArray.Length,
                    ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, rigthsByteArray);
                cmd.Parameters.AddRange(new SqlParameter[] { prm3});
                try
                {
                    connection.Open();






                    if (cmd.ExecuteNonQuery() >= 1)
                        MessageBox.Show("cheng");
                    else
                        MessageBox.Show("bai");
                    connection.Close();
                }
                catch
                {
 

                }


读取:

            //SQL命令
            string sqltxt = "Select Name From Test";
            //创建操作员实体集合
            Dictionary<string, bool> operatorCollection = new Dictionary<string, bool>();


            // 执行 SQL 命令
            using (SqlConnection conn = new SqlConnection("Initial Catalog=TestDB;User ID=sa;Password=sa;Data Source=."))
            {
                SqlCommand cmd = new SqlCommand(sqltxt, conn);
                conn.Open();


                using (SqlDataReader myReader = cmd.ExecuteReader(
                    CommandBehavior.CloseConnection))
                {
                    while (myReader.Read())
                    {
                        
                        // 读取权限集合
                        System.Data.SqlTypes.SqlBytes bytes = myReader.GetSqlBytes(0); // 只能指定列序号
                        // 将流反序列化为权限集合对象
                        BinaryFormatter bf = new BinaryFormatter();
                        if (!bytes.IsNull)
                            operatorCollection = (bf.Deserialize(bytes.Stream) as Dictionary<string, bool>);


                       
                    }
                }
            }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是C++代码实现LZ78算法的文本压缩和解压缩: ```cpp #include <iostream> #include <fstream> #include <map> #include <string> #include <bitset> using namespace std; // 压缩函数 void compress(const string &inputFilename, const string &outputFilename) { // 读取输入文件 ifstream inputFile(inputFilename, ios::binary); if (!inputFile.is_open()) { cerr << "Error: Failed to open input file.\n"; return; } string inputText((istreambuf_iterator<char>(inputFile)), istreambuf_iterator<char>()); inputFile.close(); // 构建字典 map<string, int> dictionary; int nextCode = 0; for (char c = 'a'; c <= 'z'; c++) { string s(1, c); dictionary[s] = ++nextCode; } // 压缩 ofstream outputFile(outputFilename, ios::binary); int code = 0; string prefix; for (char c : inputText) { string s = prefix + c; if (dictionary.count(s)) { prefix = s; code = dictionary[s]; } else { bitset<16> codeBits(code); outputFile.write((char*)&codeBits, sizeof(bitset<16>)); outputFile.put(c); dictionary[s] = ++nextCode; prefix.clear(); code = 0; } } if (!prefix.empty()) { bitset<16> codeBits(code); outputFile.write((char*)&codeBits, sizeof(bitset<16>)); } outputFile.close(); cout << "Compression finished.\n"; } // 解压缩函数 void decompress(const string &inputFilename, const string &outputFilename) { // 读取输入文件 ifstream inputFile(inputFilename, ios::binary); if (!inputFile.is_open()) { cerr << "Error: Failed to open input file.\n"; return; } string inputText((istreambuf_iterator<char>(inputFile)), istreambuf_iterator<char>()); inputFile.close(); // 构建字典 map<int, string> dictionary; int nextCode = 0; for (char c = 'a'; c <= 'z'; c++) { string s(1, c); dictionary[++nextCode] = s; } // 解压缩 ofstream outputFile(outputFilename, ios::binary); int code = 0; string s; for (int i = 0; i < inputText.size(); i += 3) { bitset<16> codeBits(*(uint16_t*)(inputText.c_str() + i)); code = codeBits.to_ulong(); char c = inputText[i + 2]; if (dictionary.count(code)) { s = dictionary[code]; s += c; outputFile.write(s.c_str(), s.size()); dictionary[++nextCode] = s; } else { s = dictionary[code]; s += c; outputFile.write(s.c_str(), s.size()); dictionary[++nextCode] = s; } } outputFile.close(); cout << "Decompression finished.\n"; } int main() { // 压缩 string inputFilename = "input.txt"; string outputFilename = "compressed.bin"; compress(inputFilename, outputFilename); // 解压缩 inputFilename = "compressed.bin"; outputFilename = "output.txt"; decompress(inputFilename, outputFilename); return 0; } ``` 代码中使用`map`来作为字典,其中键为字符串,值为对应的编码。压缩时,从左到右扫描输入文本,维护一个前缀字符串`prefix`,并将当前字符加到其后面得到一个新字符串`s`。如果字典中已经存在`s`,那么将`s`作为新的前缀字符串,更新编码`code`为`s`对应的编码;否则,将`(code, c)`写入输出文件,将`s`加入字典并分配新的编码,清空前缀字符串和编码。这里使用了`bitset<16>`来存储编码,因为最多只有26个字母和一个空串,需要的编码数不会超过2^16。解压缩时,从左到右扫描压缩后的文件,读取每个2字节的编码和1个字符,如果字典中已经存在该编码,则将其对应的字符串`s`加上该字符输出,并将`(code, s+c)`加入字典;否则,将`(code, s+c)`输出,并将其加入字典。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值