我有一个由第三方应用程序存储在表格中的zip文件,我无法控制。但我可以访问MySQL DB。
我想要做的是执行SELECT语句来检索blob字段并将此记录复制到另一个表中。但另一方面,我确实看到了远端的blob字段,但它不是一个zip文件,它是一个文本文件,显示System.Byte [],就是这样 - 任何有关导致此问题的任何想法以及如何解决它?
继承人我的下面 - 再次任何帮助非常感谢:)
OdbcCommand broadcastSelect = new OdbcCommand("select * from exchange where status='1' and type='UPDATE'", cloud);
OdbcDataReader DbReader = Select.ExecuteReader();
int fCount = DbReader.FieldCount;
String type = "";
String filename = "";
byte[] data = null;
int status = 0;
while (DbReader.Read())
{
if (DbReader.IsDBNull(0))
{
type = "BLANK";
}
else
{
type = (string)DbReader[0];
}
if (DbReader.IsDBNull(1))
{
filename = "BLANK";
}
else
{
filename = (string)DbReader[1];
}
if (DbReader.IsDBNull(2))
{
data = new byte[1];
}
else
{
data = (byte[])DbReader[2];
}
if (DbReader.IsDBNull(3))
{
status = 0;
}
else
{
status = (int)DbReader[3];
}
OdbcCommand Copy = new OdbcCommand("INSERT INTO exchange(type,filename,data,status) VALUES('" + type + "','" + filename + "','"
+ data + "','" + status + "')", local);
Copy.ExecuteNonQuery();
}
答案 0 :(得分:3)
使用sql参数插入二进制数据。
OdbcParameter param = new OdbcParameter("@file", SqlDbType.Binary);
---更新 我希望下面给出的代码对你有帮助。
OdbcCommand broadcastSelect = new OdbcCommand("select * from exchange where status='1' and type='UPDATE'", cloud);
OdbcDataReader DbReader = Select.ExecuteReader();
int fCount = DbReader.FieldCount;
String type = "";
String filename = "";
byte[] data = null;
int status = 0;
OdbcParameter param = null;
while (DbReader.Read())
{
if (DbReader.IsDBNull(0))
{
type = "BLANK";
}
else
{
type = (string)DbReader[0];
}
if (DbReader.IsDBNull(1))
{
filename = "BLANK";
}
else
{
filename = (string)DbReader[1];
}
if (DbReader.IsDBNull(2))
{
param = new OdbcParameter("@file", SqlDbType.Binary);
param.DbType = DbType.Binary;
param.Value = new byte[1];
command.Parameters.Add(param);
}
else
{
param = new OdbcParameter("@file", SqlDbType.Binary);
param.DbType = DbType.Binary;
param.Value = (byte[])dbReader[2];
param.Size = ((byte[])dbReader[2]).Length;
command.Parameters.Add(param);
}
if (DbReader.IsDBNull(3))
{
status = 0;
}
else
{
status = (int)DbReader[3];
}
OdbcCommand Copy = new OdbcCommand("INSERT INTO exchange(type,filename,data,status) VALUES('" + type + "','" + filename + "',@file,'" + status + "')", local);
Copy.ExecuteNonQuery();