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

翻译|其它|编辑:郝浩|2008-06-11 09:48:48.000|阅读 2297 次

概述:

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

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 ( ) ;
标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com

文章转载自:向青风博客

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP