把word文档写入Sqlserver
来源:互联网 发布:天数倒计时器单片机 编辑:程序博客网 时间:2024/05/18 04:46
//得到提交的文件
Stream fileDataStream = MyFile.PostedFile.InputStream;
//得到文件大小
int fileLength = MyFile.PostedFile.ContentLength;
//创建数组
byte[] fileData = new byte[fileLength];
//把文件流填充到数组
fileDataStream.Read(fileData,0,fileLength);
//得到文件名字
string fileTitle = MyFileName.Value;
//得到文件类型
string fileType = MyFile.PostedFile.ContentType;
//构建数据库连接,SQL语句,创建参数
SqlConnection connection = new SqlConnection("Server=.;uid=sa;pwd=mdcija;Database=TestUploadFile");
SqlCommand command = new SqlCommand ("INSERT INTO TestFiles (MyFileName,MyFile,FileType)" +
"VALUES (@MyFileName,@MyFile,@FileType)", connection);
SqlParameter paramTitle = new SqlParameter ("@MyFileName", SqlDbType.VarChar,35);
paramTitle.Value = fileTitle;
command.Parameters.Add(paramTitle);
SqlParameter paramData = new SqlParameter ("@MyFile", SqlDbType.Image);
paramData.Value = fileData;
command.Parameters.Add(paramData);
SqlParameter paramType = new SqlParameter ("@FileType", SqlDbType.VarChar,25);
paramType.Value = fileType;
command.Parameters.Add(paramType);
//打开连接,执行查询
connection.Open();
command.ExecuteNonQuery();
connection.Close();
Message.Text="你的文件已经成功上载";
MyFileName.Value = "";
Stream fileDataStream = MyFile.PostedFile.InputStream;
//得到文件大小
int fileLength = MyFile.PostedFile.ContentLength;
//创建数组
byte[] fileData = new byte[fileLength];
//把文件流填充到数组
fileDataStream.Read(fileData,0,fileLength);
//得到文件名字
string fileTitle = MyFileName.Value;
//得到文件类型
string fileType = MyFile.PostedFile.ContentType;
//构建数据库连接,SQL语句,创建参数
SqlConnection connection = new SqlConnection("Server=.;uid=sa;pwd=mdcija;Database=TestUploadFile");
SqlCommand command = new SqlCommand ("INSERT INTO TestFiles (MyFileName,MyFile,FileType)" +
"VALUES (@MyFileName,@MyFile,@FileType)", connection);
SqlParameter paramTitle = new SqlParameter ("@MyFileName", SqlDbType.VarChar,35);
paramTitle.Value = fileTitle;
command.Parameters.Add(paramTitle);
SqlParameter paramData = new SqlParameter ("@MyFile", SqlDbType.Image);
paramData.Value = fileData;
command.Parameters.Add(paramData);
SqlParameter paramType = new SqlParameter ("@FileType", SqlDbType.VarChar,25);
paramType.Value = fileType;
command.Parameters.Add(paramType);
//打开连接,执行查询
connection.Open();
command.ExecuteNonQuery();
connection.Close();
Message.Text="你的文件已经成功上载";
MyFileName.Value = "";
下载:
string sql="SELECT * FROM TestFiles WHERE ID = '" + Request.QueryString["ID"] + "'";
SqlConnection connection = new SqlConnection("Server=.;uid=sa;pwd=mdcija;Database=TestUploadFile");
SqlCommand command = new SqlCommand(sql, connection);
connection.Open();
SqlDataReader dr = command.ExecuteReader();
if(dr.Read())
{
Response.Clear();
Response.AddHeader("Content-Type",dr["FileType"].ToString());
Response.BinaryWrite((byte[])dr["MyFile"]);
}
dr.Close();
connection.Close();
SqlConnection connection = new SqlConnection("Server=.;uid=sa;pwd=mdcija;Database=TestUploadFile");
SqlCommand command = new SqlCommand(sql, connection);
connection.Open();
SqlDataReader dr = command.ExecuteReader();
if(dr.Read())
{
Response.Clear();
Response.AddHeader("Content-Type",dr["FileType"].ToString());
Response.BinaryWrite((byte[])dr["MyFile"]);
}
dr.Close();
connection.Close();
- 把word文档写入Sqlserver
- 如何把数据库的内容写入到Word文档中
- 如何把数据库的内容写入到Word文档中?
- 如何把数据库以二进制存储的excel,word文档写入excel,word
- 向服务器端写入word文档
- 网页内容写入word文档
- 用C#把数据库中的记录按格式写入Word文档中。
- 用C#把数据库中的记录按格式写入Word文档中
- 用C#把数据库中的记录按格式写入Word文档中
- 用C#把数据库中的记录按格式写入Word文档中。
- 用C#把数据库中的记录按格式写入Word文档中
- POI把html写入word doc文件
- 把word文档转化成pdf文档
- 怎么把pdf文档转成word文档
- C#中写入与保存Word文档
- Java将数据写入word文档(.doc)
- 将html内容写入word文档
- C#把Word文档转换成HTML
- Donews Ad
- Hash散列算法详细解析(四)
- Hash散列算法详细解析(五)
- JSF
- Hash散列算法详细解析(六)
- 把word文档写入Sqlserver
- Hash散列算法详细解析(七)
- JDBC连接SqlServer2005数据库
- 基于混合TCP-UDP的HTTP协议实现方法
- Hash散列算法详细解析(三)
- Hash散列算法详细解析(二)
- Hash散列算法详细解析(一)
- 访问使用自定义弹出对象的网页时,Internet Explorer 6 意外关闭
- 深入理解Linux操作系统下的守护进程