我是一位有用的助手,可以为您翻译文本。
我有一个使用C#的WinForms应用程序,将文件保存到SQL Server数据库(版本2014),保存到varbinary(MAX)字段中。
保存功能如下:
除了二进制字段,IT技术相关内容运行良好。当二进制字段被转换为文本时存在问题。
输出的XML文件。
我有一个使用C#的WinForms应用程序,将文件保存到SQL Server数据库(版本2014),保存到varbinary(MAX)字段中。
保存功能如下:
byte[] Bytefile;
using (SqlConnection conn = new SqlConnection(DataHelper.GetConnection()))
{
conn.Open();
DataTable dt = new DataTable();
SqlCommand comm = new SqlCommand("Delete T_Articale_Files where Artricle_id=" + ID, conn);
comm.ExecuteNonQuery();
foreach (string file in Directory.GetFiles(varFilePath))
{
using (var stream = new FileStream(Path.Combine(varFilePath, file), FileMode.Open, FileAccess.Read))
{
using (var reader = new BinaryReader(stream))
{
Bytefile = reader.ReadBytes((int)stream.Length);
}
}
using (var sqlWrite = new SqlCommand("INSERT INTO T_Articale_Files (Artricle_id,FileName,FileData) Values(@ID,@FileName,@File)", conn))
{
sqlWrite.Parameters.Add("@ID", SqlDbType.Int, 10).Value = ID;
sqlWrite.Parameters.Add("@FileName", SqlDbType.NVarChar, 50).Value = Path.GetFileName(file);
sqlWrite.Parameters.Add("@File", SqlDbType.VarBinary, file.Length).Value = Bytefile;
sqlWrite.ExecuteNonQuery();
}
}
}
获取函数
using (SqlConnection conn = new SqlConnection(DataHelper.GetConnection()))
// using (var varConnection = Locale.sqlConnectOneTime(Locale.sqlDataConnectionDetails))
{
conn.Open();
DataTable dt = new DataTable();
SqlCommand comm = new SqlCommand("SELECT id,FileName FROM T_Articale_Files WHERE Artricle_id = @varID", conn);
comm.Parameters.AddWithValue("@varID", varID);
dt.Load(comm.ExecuteReader());
foreach (DataRow item in dt.Rows)
{
using (var sqlQuery = new SqlCommand(@"SELECT FileData FROM T_Articale_Files WHERE id = @ID", conn))
{
sqlQuery.Parameters.AddWithValue("@ID", item["id"]);
using (var sqlQueryResult = sqlQuery.ExecuteReader())
while (sqlQueryResult.Read())
{
var blob = new Byte[(sqlQueryResult.GetBytes(0, 0, null, 0, int.MaxValue))];
sqlQueryResult.GetBytes(0, 0, blob, 0, blob.Length);
using (var fs = new FileStream(Path.Combine(varPathToNewLocation, item["FileName"].ToString()), FileMode.Create, FileAccess.Write))
fs.Write(blob, 0, blob.Length);
}
}
}
}
目前运作良好。现在我被要求将数据库转换为 XML,以便于无法连接到服务器的个人电脑使用。
转换为 XML 的功能。
var xmlFileData = "";
DataSet ds = new DataSet();
var tables = new[] { "V_Articale", "T_Articale", "T_City", "T_Classification", "T_Country", "T_Locations", "T_milishia", "T_Search", "T_statistics", "T_TerrorGroups", "T_Tribes", "T_Users", "T_Articale_Files" };
foreach (var table in tables)
{
var query = "SELECT * FROM " + table;
SqlConnection conn = GetConnection();
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable(table);
da.Fill(dt);
conn.Close();
conn.Dispose();
ds.Tables.Add(dt);
if(table== "T_Articale_Files")
{
foreach (DataRow item in dt.Rows)
{
Byte[] file = GetBytes(item["FileData"].ToString());
}
}
}
xmlFileData = ds.GetXml();
除了二进制字段,IT技术相关内容运行良好。当二进制字段被转换为文本时存在问题。
输出的XML文件。
<T_Articale_Files>
<id>6</id>
<Artricle_id>1013</Artricle_id>
<FileName>falcon banner.jpg</FileName>
<FileData>/9j/4AAQSkZJRgABAgEASABIAAD/4QleRXhpZgAATU0AKgAAAAgABwESAAMAAAABAAEAAAEaAAUAAAABAAAAYgEbAAUA</FileData>
当尝试将其转换回图像时,它会给我一个1kb的文件,里面是字符串而不是实际的图像。
是否有特殊的转换方法适用于二进制字段? 请建议。
谢谢。
编辑问题已解决,感谢 @grek40。 解决方案:转换为XML。
var xmlstream = new StringWriter();
ds.WriteXml(xmlstream, XmlWriteMode.WriteSchema);
string xmlWithSchema = xmlstream.ToString();
其中ds是数据集
转换回文件
private void databaseFileRead(int varID, string varPathToNewLocation)
{
DataSet ds = new DataSet();
ds.ReadXml(XMLpath);
DataTable dt = new DataTable();
dt = ds.Tables["T_Articale_Files"];
DataView dv = new DataView(dt);
dv.RowFilter = "Artricle_id=" + varID;
if (dv.Count > 0)
{
foreach (DataRowView item in dv)
{
byte[] stringArray = (byte[])(item["FileData"]);
File.WriteAllBytes(Path.Combine(Filepath, item["FileName"].ToString()), stringArray ); // save image to disk
}
}