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 ( ) ; 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值