从Oracle数据库下载XML文件

时间:2013-11-05 02:39:46

标签: c# xml oracle

我使用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]);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

ds_dt.Rows[0]会返回一行。您需要使用Item

的行中的项目
ds_dt.Rows[0].Item["DT_XML"]

并将其传递给CreateFile。

请参阅here for more doco on DataRow

还尝试用

替换CreateFile函数
private void CreateFile(string data)
{
    string strpath = @"D:\MyData\Example.xml";
    File.WriteAllText(strPath, data);
}

这样称呼它:

CreateFile(ds_dt.Rows[0].ItemArray["DT_XML"].ToString());