`
xqf222
  • 浏览: 117570 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

C#往SQL数据库字段中插入二进制文件的三种方法

 
阅读更多

C#往SQL数据库字段中插入二进制文件的三种方法,网络测试程序一别人好象成功了.我怎么都测试不成功.同事解说是INSERTSQL语句是字符串.所以不能把二进制内容跟字段联合起来传递给SQL2000

//创建一个SqlConnection对象
string strCon = "Initial Catalog='HMMISDATA';Server='192.168.1.180';User ID='用户名';Password='密码';Persist Security Info=True";
SqlConnection myConn = new SqlConnection ( strCon ) ;
//测试方法一:使用SQL语句插入二进制字段,测试失败
/*
string insertSQL="INSERT INTO EP_HmSoftOfficeDocList(DocumentType,DocumentDate,DocumentManager,DocumentDepartment,DocumentTitle,DocumentContent,BinaryFileData,BinaryFileType,BinaryFileLength,BinaryFilePath,AddUserName,AddUserTime,AddUserIP) ";
insertSQL=insertSQL+ " VALUES('"+DocumentType+"','"+DocumentDate+"','"+DocumentManager+"','"+DocumentDepartment+"','"+DocumentTitle+"','"+DocumentContent+"',"+@BinaryFileData+",'"+BinaryFileType+"',"+BinaryFileLength+",'"+BinaryFilePath+"','"+strAddUser+"','"+strAddTime+"','"+strAddIP+"')" ;
SqlCommand insertCommand= new SqlCommand();
insertCommand.Parameters.Add("@BinaryFileData",SqlDbType.Image);
insertCommand.Parameters["@BinaryFileData"].Value=BinaryFileData;
insertCommand.CommandType=CommandType.Text;
insertCommand.CommandText=insertSQL;
insertCommand.Connection=myConn;
insertCommand.Connection.Open();
insertCommand.ExecuteNonQuery();
*/
//测试方法二:使用存储过程插入二进制字段,测试成功
/*
SqlCommand insertCommand = new SqlCommand("sp_HmSoft_OfficeDoc_ADD",myConn);
insertCommand.CommandType = CommandType.StoredProcedure;
insertCommand.Parameters.Add(new SqlParameter("@DocumentType", SqlDbType.NVarChar, 50));
insertCommand.Parameters.Add(new SqlParameter("@DocumentDate", SqlDbType.NVarChar, 50));
insertCommand.Parameters.Add(new SqlParameter("@DocumentManager", SqlDbType.NVarChar, 50));
insertCommand.Parameters.Add(new SqlParameter("@DocumentDepartment", SqlDbType.NVarChar, 50));
insertCommand.Parameters.Add(new SqlParameter("@DocumentTitle", SqlDbType.NVarChar, 50));
insertCommand.Parameters.Add(new SqlParameter("@DocumentContent", SqlDbType.NVarChar, 50));
insertCommand.Parameters.Add(new SqlParameter("@BinaryFileData", SqlDbType.Image));
insertCommand.Parameters.Add(new SqlParameter("@BinaryFileType", SqlDbType.NVarChar, 50));
insertCommand.Parameters.Add(new SqlParameter("@BinaryFileLength", SqlDbType.Int));
insertCommand.Parameters.Add(new SqlParameter("@BinaryFilePath", SqlDbType.NVarChar, 50));
insertCommand.Parameters.Add(new SqlParameter("@AddUserName", SqlDbType.NVarChar, 50));
insertCommand.Parameters.Add(new SqlParameter("@AddUserTime", SqlDbType.NVarChar, 50));
insertCommand.Parameters.Add(new SqlParameter("@AddUserIP", SqlDbType.NVarChar, 50));
insertCommand.Parameters["@DocumentType"].Value=DocumentType;
insertCommand.Parameters["@DocumentDate"].Value=DocumentDate;
insertCommand.Parameters["@DocumentManager"].Value=DocumentManager;
insertCommand.Parameters["@DocumentDepartment"].Value=DocumentDepartment;
insertCommand.Parameters["@DocumentTitle"].Value=DocumentTitle;
insertCommand.Parameters["@DocumentContent"].Value=DocumentContent;
insertCommand.Parameters["@BinaryFileData"].Value=BinaryFileData;
insertCommand.Parameters["@BinaryFileType"].Value=BinaryFileType;
insertCommand.Parameters["@BinaryFileLength"].Value=BinaryFileLength;
insertCommand.Parameters["@BinaryFilePath"].Value=BinaryFilePath;
insertCommand.Parameters["@AddUserName"].Value=strAddUser;
insertCommand.Parameters["@AddUserTime"].Value=strAddTime;
insertCommand.Parameters["@AddUserIP"].Value=strAddIP;
insertCommand.Connection.Open();
insertCommand.ExecuteNonQuery();
*/

//测试方法三:使用DataSet插入二进制字段,测试成功
myConn.Open();
DataSet tempDataSet=new DataSet();
SqlDataAdapter tempAdapter = new SqlDataAdapter("SELECT * FROM EP_HmSoftOfficeDocList WHERE 1=0", myConn);
SqlCommandBuilder tempBuilder=new SqlCommandBuilder(tempAdapter);
tempAdapter.Fill(tempDataSet);
//'插入一条记录
DataRow tempDataRow = tempDataSet.Tables[0].NewRow();
tempDataRow["DocumentType"] =DocumentType;
tempDataRow["DocumentDate"] =DocumentDate;
tempDataRow["DocumentManager"] =DocumentManager;
tempDataRow["DocumentDepartment"] =DocumentDepartment;
tempDataRow["DocumentTitle"] =DocumentTitle;
tempDataRow["DocumentContent"] =DocumentContent;
tempDataRow["BinaryFileData"] =BinaryFileData;
tempDataRow["BinaryFileType"] =BinaryFileType;
tempDataRow["BinaryFileLength"] =BinaryFileLength;
tempDataRow["BinaryFilePath"] =BinaryFilePath;
tempDataRow["AddUserName"] =strAddUser;
tempDataRow["AddUserTime"] =strAddTime;
tempDataRow["AddUserIP"] =strAddIP;
tempDataSet.Tables[0].Rows.Add(tempDataRow);
tempAdapter.Update(tempDataSet);

//关闭连接
myConn.Close ( ) ;

分享到:
评论

相关推荐

    C# 对SqlServer中Image字段的读写(例子)

    C# 对SqlServer中Image字段的读写的思路是将图片转换为二进制在向数据库写.

    WPF SQLite存储与读取二进制图片

    WPF SQLite存储与读取二进制图片

    C#图片转换成二进制流并且保存到sql_server数据库

    C#图片转换成二进制流并且保存到sql_server数据库

    asp.net(c#)实现从sqlserver存取二进制图片的代码

    有一个员工表Employee,需要保存员工照片(Photo)到数据库(sql server)上。员工照片对应的字段是varbinary(max),也就是要存成二进制文件类型(这和以前讨巧地存图片文件路径就不相同了),默认可以为空。

    C# 文件保存到数据库中或者从数据库中读取文件

    将文件保存到数据库中,实际上是将文件转换成二进制流后,将二进制流保存到数据库相应的字段中。在SQL Server中该字段的数据类型是Image,在Access中该字段的数据类型是OLE对象。 代码如下://保存文件到SQL Server...

    MSSQL 将截断字符串或二进制数据问题的解决方法

    地图数据存放在sqlserver 2008中,使用mapxtreme7 开发时,使用Feature.Update()方法时出错的提示包含“MSSQL 将截断字符串或二进制数据” 主要原因就是给某个字段赋值时,内容大于字段的长度或类型不符造成的 解决...

    C#开发经验技巧宝典

    0976 将图片以二进制格式存储到数据库中(asp.net) 569 0977 从DataReader对象读取数据的技巧 569 0978 随机显示数据库记录 569 0979 通过DataTable获得数据表的主键 569 0980 将Access数据库转化为SQL ...

    (C#+SQL)旅游管理系统

     (5)风景图片信息表(编号,图片名称,二进制数据,景点名称,图片描述)    (6)订单信息表(订单编号,客户姓名,目的地,景区名,出发日期,返回日期,住宿标准,导游员,老人数,成人数,学生数,小孩数,总...

    Visual SQLTools 2012 Pro – 高效率SQL开发工具

    是一套专业的数据库及SQL开发工具,基于.NET Framework 和.NET Data Provider技术开发,历时十年精心打造而成,支持Oracle,SQLServer,MySQL,DB2,Sybase,PostgreSQL,Access 7种常用数据库及中日英等11种语言。...

    C#开发实例大全(基础卷).软件开发技术联盟(带详细书签) PDF 下载

    《C#开发实例大全(基础卷)》筛选、汇集了C#开发从基础知识到高级应用各个层面约600个实例及源代码,每个实例都按实例说明、关键技术、设计过程、详尽注释、秘笈心法的顺序进行了分析解读。全书分6篇共25章,主要...

    C#编程经验技巧宝典

    58 <br>0081 文本中首字母改为大写 59 <br>0082 C#随机数的产生 59 <br>0083 身份证从15位升至18位算法 60 <br>0084 十进制数转二进制数的算法 60 <br>0085 十进制数转八进制数的算法 61...

    轻松学C#(图解版)

    第三篇是应用技术篇,主要介绍的是异常处理、文件和流、委托、事件、Lambda表达式、命名空间、预处理器、程序集、运行时类型标识、反射、特性、泛型、LINQ和数据库开发等。 =======================================...

    ASP升级.net资料大全(c#入门 语言规范 源码教程 学习笔记 技术资料 面试题 asp与.net代码生成器)

    二进制数据字段的更新.txt 分页及动态创建列的控件 .txt 改变Windows服务的启动顺序.txt 各进制之间的转换.txt 给图片添加版权信息.txt 关于拖放操作.txt 关于在WinForm里用HttpWebRequest获得某个页面,并...

    SQL Server字符串比较时区别大小写方法

    在SQL Server中默认对大小写是不敏感的,例如userName=""jesse""和userName=""JESSE""结果是一样的。在验证密码的时候可能需要对字符串大小写敏感,需要做一些处理,介绍两种方法:  法Ⅰ:转换成二进制再比较,...

Global site tag (gtag.js) - Google Analytics