1

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

1 回答 1

0

ds_dt.Rows[0]返回一行。您需要使用该行中的项目Item

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

并将其传递给 CreateFile。

有关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());
于 2013-11-05T03:29:34.920 回答