我已使用 C#.Net 4 成功将 XML 文件保存到 oracle 数据库,现在我想下载该文件,代码如下:
public bool DwndData(object id, out DataTable data, out string message)
{
_dataadapter.SelectCommand.Parameters.Clear();
_dataadapter.SelectCommand.CommandText = "SELECT DT_XML FROM EKG.MY_TABLE WHERE ID=:MY_ID";
_dataadapter.SelectCommand.Parameters.Add(":MY_ID", id);
try
{
data = new DataTable("MY DOWNLOAD");
_dataadapter.Fill(data);
message = "Get data success.";
return true;
}
catch (OracleException e)
{
data = null;
message = "Get data Error.\n MSG :" + e.Message;
return false;
}
}
private void CreateFile(object FileValue)
{
string strpath = @"D:\MyData\Example.xml";
FileStream fs;
int longfile = ObjectToByteArray(FileValue).Length;
if (!Directory.Exists(strpath))
{
Directory.CreateDirectory(Path.GetDirectoryName(strpath));
fs = new FileStream(@strpath, FileMode.Create);
fs.Write(ObjectToByteArray(FileValue), 1000, longfile);
fs.Close();
}
else
{
fs = new FileStream(@strpath, FileMode.Create);
fs.Write(ObjectToByteArray(FileValue), 1000, longfile);
fs.Close();
}
}
private byte[] ObjectToByteArray(Object obj)
{
if (obj == null)
return null;
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, obj);
return ms.ToArray();
}
private void download_data_Click(object sender, EventArgs e)
{
DataTable ds_dt;
foreach (DataGridViewRow cell in datagridview1.SelectedRows)
{
if (!DwndData(datagridview1.Rows[cell.Index].Cells["ID"].Value, out ds_dt, out msg))
MessageBox.Show(msg);
if (ds_dt.Rows.Count > 0)
{
CreateFile(ds_dt.Rows[0]);
}
}
}