最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
使用OleDbCommand对象更新SQL Server中的二进制文件
时间:2022-07-02 18:06:59 编辑:袖梨 来源:一聚教程网
使用OleDbCommand对象更新SQL Server中的二进制文件
作者 朱二
利用ADO.NET中的OleDbConnectionOleDbCommand 可以方便的对SQL Server中的二进制文件进行更新操作,下面是详细的代码演示
演示环境:
数据库机器名 :s_test
登陆名 :sa
密码 :7890
数据库名 db_test
下面建立一个表:
create table tb_test(id int identity(1,1),photo image ,constraint pk_tb_test primary key(id))
一、将硬盘上的文件保存至数据库(C#)
//----------------------------------------------------------
//----------------------------------------------------------
//下面的示例将c:1.txt文件保存至数据库的tb_test表中
//----------------------------------------------------------
//----------------------------------------------------------
using System;
using System.IO;?
using System.Data;
using System.Data.OleDb;
class image_test
{
[STAThread]
static void Main(string[] args)
{
try
{
//初始化OleDbConnection和OleDbCommand
OleDbConnection cn = new OleDbConnection("provider=sqloledb;server=s_test;user id=sa;password=7890;initial catalog=db_test");
OleDbCommand cmd = new OleDbCommand("INSERT tb_test(photo) VALUES(?)",cn);
//打开文件
FileStream fs = new FileStream("c:1.txt", FileMode.Open, FileAccess.Read);
Byte[] b = new Byte[fs.Length];
fs.Read(b, 0, b.Length);
fs.Close();
//打开连接
OleDbParameter prm = new OleDbParameter("@photo",OleDbType.VarBinary ,b.Length,?
ParameterDirection.Input, false, 0, 0, null,DataRowVersion.Current, b);
cmd.Parameters.Add(prm);
cn.Open();
//执行
if (cmd.ExecuteNonQuery() == 1)
Console.WriteLine("OK");
else
Console.WriteLine("Fail");?
cn.Close();
}
catch(Exception ex)
{
Console.WriteLine(ex.Message );
}
}
}?
三、更新数据库中保存的文件
//----------------------------------------------------------
作者 朱二
利用ADO.NET中的OleDbConnectionOleDbCommand 可以方便的对SQL Server中的二进制文件进行更新操作,下面是详细的代码演示
演示环境:
数据库机器名 :s_test
登陆名 :sa
密码 :7890
数据库名 db_test
下面建立一个表:
create table tb_test(id int identity(1,1),photo image ,constraint pk_tb_test primary key(id))
一、将硬盘上的文件保存至数据库(C#)
//----------------------------------------------------------
//----------------------------------------------------------
//下面的示例将c:1.txt文件保存至数据库的tb_test表中
//----------------------------------------------------------
//----------------------------------------------------------
using System;
using System.IO;?
using System.Data;
using System.Data.OleDb;
class image_test
{
[STAThread]
static void Main(string[] args)
{
try
{
//初始化OleDbConnection和OleDbCommand
OleDbConnection cn = new OleDbConnection("provider=sqloledb;server=s_test;user id=sa;password=7890;initial catalog=db_test");
OleDbCommand cmd = new OleDbCommand("INSERT tb_test(photo) VALUES(?)",cn);
//打开文件
FileStream fs = new FileStream("c:1.txt", FileMode.Open, FileAccess.Read);
Byte[] b = new Byte[fs.Length];
fs.Read(b, 0, b.Length);
fs.Close();
//打开连接
OleDbParameter prm = new OleDbParameter("@photo",OleDbType.VarBinary ,b.Length,?
ParameterDirection.Input, false, 0, 0, null,DataRowVersion.Current, b);
cmd.Parameters.Add(prm);
cn.Open();
//执行
if (cmd.ExecuteNonQuery() == 1)
Console.WriteLine("OK");
else
Console.WriteLine("Fail");?
cn.Close();
}
catch(Exception ex)
{
Console.WriteLine(ex.Message );
}
}
}?
三、更新数据库中保存的文件
//----------------------------------------------------------
相关文章
- 《一耽女孩》官网入口地址直达 12-21
- 蓝莓影院官网最新入口-2026蓝莓影院电视剧全网免费观看 12-21
- 阿里旺旺网页版在线登录入口-阿里旺旺网页版官方唯一在线入口 12-21
- 学习通网页版登录入口 学校通官方免费在线使用指南 12-21
- 抖币充值官网入口-抖音1:10比例抖币直充链接 12-21
- 百度云盘网页版直达入口-百度网盘网页版快捷登录入口 12-21