微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我必须在我的数据库中保留.csv,但对于更可测试的应用程序,我更喜欢不使用过程.
基本上我只生成一个文件,下一条指令放在数据库中.

有人在代码中有一些关于最佳方法的线索吗?

解决方法

这是一个使用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 setup
string 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 settings
String SourceLoc  = "D:/Images/photo.jpg";
String DestinationLoc = "D:/Images/Testimage.jpg";

// provide read access to the file

FileStream fs = new FileStream(SourceLoc,FileMode.Open,FileAccess.Read);

// Create a byte array of file stream length
byte[] ImageData = new byte[fs.Length];

//Read block of bytes from stream into the byte array
fs.Read(ImageData,System.Convert.ToInt32(fs.Length));

//Close the File Stream
fs.Close();

// Step 3
// Create Anonymous PL/sql block string
String 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 Block
OracleCommand cmd = new OracleCommand();
cmd.CommandText = block;
cmd.Connection = con;


// Since executing an anonymous PL/sql block,setting the command type
// as Text instead of StoredProcedure
cmd.CommandType = CommandType.Text;

// Step 4
// Setting Oracle parameters

// Bind the parameter as OracleDbType.Blob to command for inserting image
OracleParameter param = cmd.Parameters.Add("blobtodb",OracleDbType.Blob);
param.Direction = ParameterDirection.Input;


// Assign Byte Array to Oracle Parameter
param.Value = ImageData;

// Bind the parameter as OracleDbType.Blob to command for retrieving the image
OracleParameter 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 parameter
cmd.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 array
byte[] byteData = new byte[0];

// fetch the value of Oracle parameter into the byte array
byteData = (byte[])((OracleBlob)(cmd.Parameters[1].Value)).Value;

// get the length of the byte array
int ArraySize = new int();
ArraySize = byteData.GetUpperBound(0);

// Write the Blob data fetched from database to the filesystem at the
// destination location
FileStream 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' ");

原文地址:https://www.jb51.cc/csharp/97155.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐