CSharp读写SQL Server数据库中的Image列

2016-01-29 12:53 130 1 收藏

CSharp读写SQL Server数据库中的Image列,CSharp读写SQL Server数据库中的Image列

【 tulaoshi.com - ASP.NET 】


数据库表结构:
IF EXISTS (SELECT name FROM sysobjects WHERE name = 'Content' AND type = 'U')
DROP TABLE Content
GO
create table Content
(
--内容ID
Id bigint IDENTITY(1,1) NOT NULL,
--内容
data image default null,
CONSTRAINT PK_ContentId PRIMARY KEY CLUSTERED (Id)
)
GO
const String cnnstr = "Provider=SQLOLEDB.1;User ID=sa;Password=;Initial Catalog=MyDb;Data Source=(local)";
写入
public bool WriteContent(byte [] data)
{
OleDbConnection conn = new OleDbConnection(cnnstr);
String mySelectQuery = "insert into Content(data)values(?)";
OleDbCommand myCommand = new OleDbCommand(mySelectQuery, conn);
myCommand.CommandTimeout = 60;


OleDbParameter param = new OleDbParameter("@data", OleDbType.VarBinary, data.Length);
param.Direction = ParameterDirection.Input;
param.Value = data;

myCommand.Parameters.Add(param);

bool ret = false;
try
{
conn.Open();
myCommand.ExecuteNonQuery();
ret = true;
}
catch(Exception e)
{
String msg = e.Message;
}
finally
{
//关闭数据库连接
if((conn != null) && (conn.State != ConnectionState.Closed))
{
conn.Close();
}
}
return ret;
}
读出
private byte [] ReadContent(Int64 contentId)
{
byte [] buff = null;
OleDbConnection conn = new OleDbConnection(cnnstr );
String queryStr = String.Format("select data from Content where Id={0}", contentId);
OleDbCommand myCommand = new OleDbCommand(queryStr, conn);
myCommand.CommandTimeout = 60;
OleDbDataReader reader = null;
long dataLen = 0L;
try
{
conn.Open();
reader = myCommand.ExecuteReader();
if( reader.Read() )
{
//得到要读的数据长度,注意buff必须是空的引用
dataLen = reader.GetBytes(0, 0, buff, 0, 1);
//分配缓冲区
buff = new byte[dataLen];
//读数据
dataLen = reader.GetBytes(0, 0, buff, 0, (int)dataLen);
}
}
catch(Exception e)
{
internalError(e);
String msg = e.Message;
}
finally
{
//关闭数据集
if(reader != null && !reader.IsClosed)
reader.Close();
//关闭数据库连接
if((conn != null) && (conn.State != ConnectionState.Closed))
{
conn.Close();
}
}
return buff; }

来源:https://www.tulaoshi.com/n/20160129/1487946.html

延伸阅读
标签: SQLServer
在计算机中数据有两种特征:类型和长度。所谓数据类型就是以数据的表现方式和存储方式来划分的数据的种类。     在SQL Server 中每个变量、参数、表达式等都有数据类型。系统提供的数据类型分为几大类,如表4-2 所示。     其中,BIGINT、 SQL_VARIANT 和TABLE 是SQL Server 2000 中新增加的3...
标签: SQLServer
4.5.1 注释符(Annotation) 在Transact-SQL 中可使用两类注释符。 ANSI 标准的注释符“--” 用于单行注释; 与C语言相同的程序注释符号,即“/**/”。“/*”用于注释文字的开头,“*/”用于注释文字的结尾,可在程序中标识多行文字为注释。 4.5.2 运算符(Operator)     1 算术运算符 包括:+(加)、―(...
标签: SQLServer
Enterprise Manager 提供了可视化的界面,在其中建立数据库及其对象,如表、视图、缺省值等,很少需要用户自己编辑程序代码。但对用户来说,了解这些对象是如何通过SQL语言建立,的并得到其SQL 语言脚本(Script) 是很有好处。的在Enterprise Manager 中提供了工具,以帮助用户产生这些对象的SQL 语言脚本。 生成对象的SQL 脚本方法如下: (1) ...
标签: SQLServer
4.7.1 BACKUP BACKUP 命令用于将数据库内容或其事务处理日志备份到存储介质上(软盘、硬盘、磁带)。等SQL Server 7.0 以前的版本用的是DUMP 命令来执行此功能,从SQL Server 2000起,不再使用DUMP 命令。关于BACKUP 命令的详情请参见“数据备份与恢复”章节。      4.7.2 CHECKPOINT 语法如下: CHECKPOINT CH...
标签: SQLServer
在企业管理器中可以很方便地调用其它SQL Server 工具,如SQL Server Query Analyzer (查询分析器)、SQL Server Profiler (跟踪器)等,只须从“Tools (工具)”菜单中选择相应的工具即可。 SQL Server 2000 中提供了大量的向导工具,可以引导用户完成一系列的数据库与服务器管理工作。可以从“Tools (工具)”菜单中选择“Wizards” 选项,或从工...

经验教程

618

收藏

75
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部