【问题标题】:Getting BLOB file from oracle in Windows Form C#在 Windows 窗体 C# 中从 oracle 获取 BLOB 文件
【发布时间】:2021-12-01 05:54:59
【问题描述】:

我是 C# 中的菜鸟,并尝试创建项目以从 oracle 以 win 形式下载 blob 文件并将文件保存在本地下载文件夹中。

这是我的代码

string tempDir = "\\\\NB17-KP-239\\Downloads\\";
                        for (int index = 0; index < dataTable.Rows.Count; ++index)
                        {

                            string oradbConString = "Data Source = localhost; Persist Security Info = True; User ID = homeuser; Password = admin;";
                            OracleConnection oraCon = new OracleConnection(oradbConString);
                            oraCon.Open();

                            string path;
                            using (oraCon)
                            {
                                using (OracleCommand comOra = oraCon.CreateCommand())
                                {
                                    comOra.CommandText = "select id,name,contenttype from blob_sample where id = 3";
                                    //comOra.Parameters.Add("Id", dataTable.Rows[index]["id"]);

                                    OracleDataReader oracleDataReader = comOra.ExecuteReader();
                                    oracleDataReader.Read();
                                    OracleBlob oracleBlob = oracleDataReader.GetOracleBlob(1);

                                    using (TempFileCollection tempFileCollection = new TempFileCollection(tempDir, false))
                                    {
                                        path = tempFileCollection.AddExtension("file", true);
                                        FileStream fileStream = new FileStream(path, FileMode.Create);
                                        byte[] buffer = new byte[oracleBlob.Length];
                                        int count = oracleBlob.Read(buffer, 0, Convert.ToInt32(oracleBlob.Length));
                                        fileStream.Write(buffer, 0, count);
                                        fileStream.Close();
                                    }
                                }
                                oraCon.Close();
                            }

这有什么问题? 如果我尝试一个简单的代码来选择文件并将其显示在标签上,则代码工作正常。 提前致谢

【问题讨论】:

    标签: asp.net winforms oracle11g


    【解决方案1】:

    我找到了:问题是我在 GetOracleBlob 数组中选择了元素 (1),而正确的元素是 (0)。

    string tempDir = string tempDir = @"D:\Test";
                            for (int index = 0; index < dataTable.Rows.Count; ++index)
                            {
                                //this.AppendText("download start : " + reqBy + " < " + appBy + "\r\n");
    
                                string oradbConString = "Data Source = localhost; Persist Security Info = True; User ID = homeuser; Password = admin;";
                                
                                OracleConnection oraCon = new OracleConnection(oradbConString);
                                oraCon.Open();
    
                                string path;
                                using (oraCon)
                                {
                                    using (OracleCommand comOra = oraCon.CreateCommand())
                                    {
    
                                        comOra.CommandText = "select data from blob_sample where id = 4";
    
                                        OracleDataReader oracleDataReader = comOra.ExecuteReader();
                                        oracleDataReader.Read();
                                        OracleBlob oracleBlob = oracleDataReader.GetOracleBlob(0);
    
                                        using (TempFileCollection tempFileCollection = new TempFileCollection(tempDir, false))
                                        {
                                            path = tempFileCollection.AddExtension("file", true);
                                            FileStream fileStream = new FileStream(path, FileMode.Create);
                                            byte[] buffer = new byte[oracleBlob.Length];
                                            int count = oracleBlob.Read(buffer, 0, Convert.ToInt32(oracleBlob.Length));
                                            fileStream.Write(buffer, 0, count);
                                            fileStream.Close();
                                        }
                                    }
                                    oraCon.Close();
                                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-04
      • 2015-01-29
      • 2010-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多