VS通过Oracle驱动连接数据库,对图片进行存取操作(Oracle.DataAccess.Client.dll)

通过Oracle.DataAccess.Client.dll访问数据库时,若本机Oracle客户端的版本是64位,可能会报以下错误:
未能加载文件或程序集“Oracle.DataAccess, Version=2.112.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342”或它的某一个依赖项。试图加载格式不正确的程序
原因是服务器的与本机的Oracle客户端的版本不同,解决方法:右击项目,选择【属性】->【生成】,将目标平台改为x64即可。
代码如下:
 
  
            //从本地获取图片字节数组
            Byte[] imgbyte = null;
            string imagPath = @"C:\Users\Public\Pictures\Sample Pictures\aaa.jpg";
            using(FileStream fs = new FileStream(imagPath, FileMode.Open, FileAccess.Read))
            {
                imgbyte = new Byte[fs.Length];
                fs.Read(imgbyte, 0, imgbyte.Length);
                fs.Close();
            }
            
            //通过Oracle公司提供的驱动连接数据库,Oracle.DataAccess.Client.dll可从Oracle客户端的安装路径(...\dbhome_1\ODP.NET\bin\2.x)获取
            string connString = "Data Source = datasource;User ID = userid;Password = password;";
            //string connString = "Data Source = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = ip)(PORT = 1521))(CONNECT_DATA = (SERVER = DEDICATED)(SERVICE_NAME = servername)));User ID = userid;Password = password;";
            Oracle.DataAccess.Client.OracleConnection con = new Oracle.DataAccess.Client.OracleConnection(connString);
            Oracle.DataAccess.Client.OracleCommand cmd = new Oracle.DataAccess.Client.OracleCommand();
            cmd.CommandText = "insert into tbl_picture(id,picture,createtime) values(666,:picture,sysdate)";
            cmd.Parameters.Add("picture", OracleDbType.Blob).Value = imgbyte;
            cmd.Connection = con;
            try
            {
                con.Open();
                //图片存入数据库
                int count = cmd.ExecuteNonQuery();
                Console.WriteLine("图片存储成功,"+count+"行受影响!");

                //从数据库获取图片字节数组并写入本地
                cmd.CommandText = "select picture from tbl_picture where id = 666";
                Oracle.DataAccess.Client.OracleDataReader reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    byte[] iBytes = reader.GetFieldValue(0);
                    Console.WriteLine("从数据库获取图片成功!");
                    //写入本地
                    string filePath = @"C:\TestPicture\";
                    if (!Directory.Exists(filePath)) Directory.CreateDirectory(filePath);
                    string fileName = filePath+"aaafb.jpg";
                    using (FileStream fss = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                    {
                        fss.Write(iBytes, 0, iBytes.Length);
                        fss.Close();
                        Console.WriteLine("图片已写入本地,路径:"+filePath);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("异常抛出:" + ex.Message);
            }
            finally
            {
                con.Close();
            }


 
 

你可能感兴趣的:(图片处理,.net)