sql 获得blob oracle_C# 实现数据库blob字段的存取

1、用C#实现Oracle数据库BLOB字段与本地照片文件的存取操作

1、从数据库读取BLOB数据写入本地文件

// strid 主键字段名 strzp 照片字段名 strtb 数据库表名 strpath 存放照片的本地路径

byte[] img = new byte[0];

OracleCommand cmd = new OracleCommand();

OracleDataReader rs;

cmd.Connection = conn;

cmd.CommandType = CommandType.Text;

cmd.CommandText = "select " + strid + ", " + strzp + " from " + strtb;

rs = cmd.ExecuteReader();

while (rs.Read())

{

img = (byte[])rs[1];

System.IO.File.WriteAllBytes(strpath + "" + rs[0].ToString() + ".jpg", img);

}

rs.Close();

2、将本地图片文件存入数据库

//参数说明:数据库表名,主键字段名,照片字段名,写入主键值,本地照片文件名,含完整路径

private Boolean insertpic(string d_tb, string d_id, string d_zp, string id, string filename)

{

int result = 0;

string strsql = "insert into " + d_tb + "(" + d_id + "," + d_zp + ") values('" + id + "', :pic)";

OracleCommand cmd = new OracleCommand();

cmd.Connection = conn;

cmd.CommandText = strsql;

FileStream fs = System.IO.File.OpenRead(filename);

byte[] imagebyte = new byte[fs.Length];

fs.Read(imagebyte, 0, (int)fs.Length);

cmd.Parameters.Add(new OracleParameter(":pic", imagebyte));

try

{

result = cmd.ExecuteNonQuery();

}

catch

{result = 0; }

if (result == 1)

return true;

else

return false;

}

原文链接:https://blog.csdn.net/shaowl111/article/details/80521129

2、用C#实现MySql数据库BLOB字段的读取

读数据:

public List GetList(string typeAuthorization)

{

string selectAll = “需要执行的SQl语句”

List authorizationInfoList = null;

using (MySqlDataReader reader = DAL.MySqlHelper.ExecuteReader(CommandType.Text, selectAll))

{

if (reader != null)

{

authorizationInfoList = new List();

while (reader.Read())

{

AuthorizationInfo info = new AuthorizationInfo();

info.ID = reader.GetInt32(0);

info.InstrumentID = reader.IsDBNull(1) ? 0 : reader.GetInt32(1);

//此处省略其他字段……..

info.MachineID_crypt = reader.IsDBNull(17) ? null : (byte[])reader.GetValue(17);

authorizationInfoList.Add(info);

}

}

}

return authorizationInfoList;

}

写数据:

public int Add(AuthorizationInfo authorizationInfo, string Type)

{

string sqlStr = “执行的sql语句”;

MySqlParameter[] mySqlParameters =

{

new MySqlParameter("@MachineID_crypt",MySqlDbType.Blob),

};

mySqlParameters[10].Value = authorizationInfo.MachineID_crypt;

object obj = DAL.MySqlHelper.ExecuteScalar(DAL.MySqlHelper.Conn, CommandType.Text, sqlStr, mySqlParameters);

if (obj == null)

{

return 0;

}

else

{

return Convert.ToInt32(obj);

}

}

6d1d0b3ad6994c1ab96fccbfb1b862e9.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
blob数据存入MySQL数据库的text字段可以通过以下步骤实现: 1. 将blob数据转换为字符串。 2. 创建一个MySQL连接对象。 3. 使用pandas将数据读入dataframe中。 4. 将dataframe中的blob数据列转换为字符串。 5. 将dataframe中的数据插入到MySQL数据库的text字段中。 下面是一个示例代码,可以将一个包含blob数据的dataframe存储到MySQL数据库的text字段中: ```python import pandas as pd import mysql.connector # 将blob数据转换为字符串 def blob_to_string(blob): return str(blob, 'utf-8') # 创建MySQL连接对象 conn = mysql.connector.connect(host='localhost', user='root', password='password', database='mydb') # 读入数据并将blob数据列转换为字符串 df = pd.read_csv('data.csv') df['blob_data'] = df['blob_data'].apply(blob_to_string) # 将dataframe中的数据插入到MySQL数据库的text字段中 cursor = conn.cursor() for index, row in df.iterrows(): sql = "INSERT INTO mytable (id, text_data) VALUES (%s, %s)" val = (row['id'], row['blob_data']) cursor.execute(sql, val) conn.commit() # 关闭MySQL连接 conn.close() ``` 在上面的代码中,我们首先定义了一个函数`blob_to_string`来将blob数据转换为字符串。然后,我们创建了一个MySQL连接对象,并使用pandas将数据读入dataframe中。接下来,我们将dataframe中的blob数据列转换为字符串,然后使用循环逐行将数据插入到MySQL数据库的text字段中。最后,我们关闭了MySQL连接。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值