【问题标题】:Problems with import BLOB from old database to new in C# SQLite在 C# SQLite 中将 BLOB 从旧数据库导入新数据库的问题
【发布时间】:2018-09-09 11:06:56
【问题描述】:

在将旧数据库导入具有不同结构的新数据库时,我遇到了很大的问题。

在我的旧版本中:

表:CHART_DATA

CHART_ID - 整数、主键、自增

CHART_DATA - 带有二进制文件的 BLOB

文件的十六进制视图示例:01000000630AC2BF332934BF79E08D43EFF13645

从旧数据库中选择:

    sql = "SELECT hex(CHART_DATA) FROM CHART_DATA";
            SQLiteCommand select_hex = new SQLiteCommand(sql.ToString(), old_dbConnection);
            reader = select_hex.ExecuteReader();
    int i = 0;
    while (reader.Read())
            {
                chart_hex[i] = reader[0].ToString();
                i++;
            }

当我尝试将其保存到新基地时,我遇到了很多问题:

第一次尝试:将字符串保存到 BLOB 列:

for (int k = 0; k < count; k++)
        {
            SQLiteCommand command = new SQLiteCommand(sqlinsert.ToString(), new_dbConnection);
            command.CommandText = "INSERT INTO chart(chart_file) VALUES (@chart_file)";
            command.Parameters.AddWithValue("@chart_file", chart_hex[k]]);
        }

结果是可预测的,列中的字符串值:((例如:01000000630AC2BF332934BF79E08D43EFF13645)

第二次尝试:另存为 DbType.Binary

for (int k = 0; k < count; k++)
        {
            SQLiteCommand command = new SQLiteCommand(sqlinsert.ToString(), new_dbConnection);
            command.CommandText = "INSERT INTO chart(chart_file) VALUES (@chart_file)";
             command.Parameters.Add("@chart_file", DbType.Binary, 20).Value = chart_hex[k];
        }

同样的结果:((字符串:01000000630AC2BF332934BF79E08D43EFF13645)

第三次尝试:字符串到字节的转换

 for (int k = 0; k < count; k++)
            {
                SQLiteCommand command = new SQLiteCommand(sqlinsert.ToString(), new_dbConnection);
                command.CommandText = "INSERT INTO chart(chart_file) VALUES (@chart_file)";
                command.Parameters.Add("@chart_file", DbType.Binary, 20).Value = GetBytes(chart_hex[k]);
                //command.Parameters.AddWithValue("@chart_file", GetBytes(chart_hex[k, 1])); //I also try this 

            }

            ...

             static byte[] GetBytes(string str)
            {
                byte[] bytes = new byte[str.Length * sizeof(char)];
                System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
                return bytes;
            }

结果是数据库中的 BLOB 对象但有问题。文件与原文件不同:

原文:01000000630AC2BF332934BF79E08D43EFF13645

在新数据库中:

31003000300030003000300030003600
33003000410043003200420046003300
33003200390033003400420046003700
39004500300038004400340033004500
4600460031003300360034003500

我已经不知道如何导入这个表单旧数据库了:(

【问题讨论】:

    标签: c# sqlite binary blob database-migration


    【解决方案1】:

    如果您不需要字符串,则不要选择字符串。将目标数组更改为object 数组并获取存储在旧数据库中的值。这假定 BLOB 数据实际上并未作为文本存储在旧数据库中。

    string sql = "SELECT CHART_DATA FROM CHART_DATA";
    SQLiteCommand select_hex = new SQLiteCommand(sql, old_dbConnection);
    reader = select_hex.ExecuteReader();
    int i = 0;
    while (reader.Read())
    {
      // chart_hex is changed to an object array, not a string array
      chart_hex[i] = reader[0];
      i++;
    }
    

    然后,只需插入对象值:

    for (int k = 0; k < count; k++)
    {
      SQLiteCommand command = new SQLiteCommand(new_dbConnection);
      command.CommandText = "INSERT INTO chart(chart_file) VALUES (@chart_file)";
      command.Parameters.AddWithValue("@chart_file", chart_hex[k]]);
    }
    

    SQLiteParameter 类应在两个方向上将对象值映射到 BLOB 列关联,从而以相同的格式检索和存储数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 2018-04-14
      • 1970-01-01
      • 2014-03-12
      • 1970-01-01
      • 1970-01-01
      • 2014-01-23
      相关资源
      最近更新 更多