简体   繁体   English

从Oracle数据库下载XML文件

[英]Download XML File From Oracle Database

I Have save my XML file to oracle database successful with C#.Net 4 and now i would like to download that file, here is the code: 我已经用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]);
        }
    }
}

ds_dt.Rows[0] returns a row. ds_dt.Rows[0]返回一行。 You need the item in the row using Item 您需要使用Item行中的Item

ds_dt.Rows[0].Item["DT_XML"]

and pass that into CreateFile. 并将其传递给CreateFile。

See here for more doco on DataRow 在此处查看有关DataRow的更多资料

Also try replacing the CreateFile function with 也可以尝试将CreateFile函数替换为

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

calling it like this: 这样称呼它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM