使用C#在oracle数据库中插入blob

使用C#在oracle数据库中插入blob,第1张

概述我必须在我的数据库中保留.csv,但对于更可测试的应用程序,我更喜欢不使用过程. 基本上我只生成一个文件,下一条指令放在数据库中. 有人在代码中有一些关于最佳方法的线索吗? 这是一个使用c#和过程在oracle中插入blob数据的示例(你说更喜欢这意味着你可能). using System;using System.Data;using Oracle.DataAccess.Client;us 我必须在我的数据库中保留.csv,但对于更可测试的应用程序,我更喜欢不使用过程.
基本上我只生成一个文件,下一条指令放在数据库中.

有人在代码中有一些关于最佳方法的线索吗?@H_419_4@解决方法 这是一个使用c#和过程在oracle中插入blob数据的示例(你说更喜欢这意味着你可能).

using System;using System.Data;using Oracle.DataAccess.ClIEnt;using Oracle.DataAccess.Types;using System.IO;using System.Text;//Step 1// Connect to database// Note: Modify User ID,Password,Data Source as per your database setupstring constr = "User ID=Scott;Password=tiger;Data Source=orcl9i";OracleConnection con = new OracleConnection(constr);con.open();Console.Writeline("Connected to database!");// Step 2// Note: Modify the Source and Destination location// of the image as per your machine settingsString SourceLoc  = "D:/Images/photo.jpg";String DestinationLoc = "D:/Images/Testimage.jpg";// provIDe read access to the filefileStream fs = new fileStream(SourceLoc,fileMode.Open,fileAccess.Read);// Create a byte array of file stream lengthbyte[] ImageData = new byte[fs.Length];//Read block of bytes from stream into the byte arrayfs.Read(ImageData,System.Convert.ToInt32(fs.Length));//Close the file Streamfs.Close();// Step 3// Create Anonymous PL/sql block stringString block = " BEGIN " +               " INSERT INTO testblob (ID,photo) VALUES (100,:1); " +               " SELECT photo into :2 from testblob WHERE ID = 100; " +               " END; ";// Set command to create Anonymous PL/sql BlockOracleCommand cmd = new OracleCommand();cmd.CommandText = block;cmd.Connection = con;// Since executing an anonymous PL/sql block,setting the command type// as Text instead of StoredProcedurecmd.CommandType = CommandType.Text;// Step 4// Setting Oracle parameters// Bind the parameter as OracleDbType.Blob to command for inserting imageOracleParameter param = cmd.Parameters.Add("blobtodb",OracleDbType.Blob);param.Direction = ParameterDirection.input;// Assign Byte Array to Oracle Parameterparam.Value = ImageData;// Bind the parameter as OracleDbType.Blob to command for retrIEving the imageOracleParameter param2 = cmd.Parameters.Add("blobfromdb",OracleDbType.Blob);param2.Direction = ParameterDirection.Output;// Step 5// Execute the Anonymous PL/sql Block// The anonymous PL/sql block inserts the image to the// database and then retrIEves the images as an output parametercmd.ExecuteNonquery();Console.Writeline("Image file inserted to database from " + SourceLoc);// Step 6// Save the retrIEved image to the DestinationLoc in the file system// Create a byte arraybyte[] byteData = new byte[0];// fetch the value of Oracle parameter into the byte arraybyteData = (byte[])((OracleBlob)(cmd.Parameters[1].Value)).Value;// get the length of the byte arrayint ArraySize = new int();ArraySize = byteData.GetUpperBound(0);// Write the Blob data fetched from database to the filesystem at the// destination locationfileStream fs1 = new fileStream(@DestinationLoc,fileMode.OpenorCreate,fileAccess.Write);fs1.Write(byteData,ArraySize);fs1.Close();Console.Writeline("Image saved to " + DestinationLoc + " successfully !");Console.Writeline("");Console.Writeline("***********************************************************");Console.Writeline("Before running this application again,execute 'Listing 1' ");
总结

以上是内存溢出为你收集整理的使用C#在oracle数据库中插入blob全部内容,希望文章能够帮你解决使用C#在oracle数据库中插入blob所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1237585.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-06
下一篇2022-06-06

发表评论

登录后才能评论

评论列表(0条)

    保存