首页 > 解决方案 > 在 Windows 窗体 C# 中从 oracle 获取 BLOB 文件

问题描述

我是 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.netwinformsoracle11g

解决方案


我找到了:问题是我在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();
                            }

推荐阅读