使用c#在mysql数据库中插入doc文件 [英] inserting a doc file in a mysql database using c#

查看:74
本文介绍了使用c#在mysql数据库中插入doc文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用网页中的文件上传控件上传了一个文件。我想将其插入数据库(MySQL)。我该如何实现?我也可以同样插入图像吗?

I have uploaded a file using the File Upload control in the webpage. I want to insert this into the database (MySQL). How do I achieve it? Can I do the same for inserting images as well?

推荐答案

看看这个使用Connector / Net处理BLOB数据 [ ^ ]


For inserting image in to database we have to convert our image in fileupload in to byte array.
And make sure that our database column will support images. If not change the column type in to image type.





f(fuBrowse.PostedFile .ContentLength> 1)-----> 1

{

FileUpload img =(FileUpload)fuBrowse;

Byte [] imgByte = null;

if(fuBrowse.HasFile&& fuBrowse.PostedFile!= null)

{



HttpPostedFile File = fuBrowse.PostedFile; ----> 2



imgByte = new Byte [File.ContentLength]; ---> 3 < br $>


File.InputStream.Read(imgByte,0,File.ContentLength);





insertCommand.Parameters.AddWithValue(@ image,imgByte);

insertCommand.ExecuteNonQuery();

}

}



1.我们正在检查文件上传包含文件。 fuBrowse是文件上传控件。

2.将fileupload中的文件转换为HttpPostedFile

3.我们正在将此HttpPostedFile转换为字节数组。

4.使用commandName.Parameter.addWithValue将字节数组传递给数据库。



f (fuBrowse.PostedFile.ContentLength > 1) ----->1
{
FileUpload img = (FileUpload)fuBrowse;
Byte[] imgByte = null;
if (fuBrowse.HasFile && fuBrowse.PostedFile != null)
{

HttpPostedFile File = fuBrowse.PostedFile;---->2

imgByte = new Byte[File.ContentLength];--->3

File.InputStream.Read(imgByte, 0, File.ContentLength);


insertCommand.Parameters.AddWithValue("@image", imgByte);
insertCommand.ExecuteNonQuery();
}
}

1.Here we are checking the file upload contain file. fuBrowse is the file upload control.
2. Converting the file in fileupload in to HttpPostedFile
3.And we are converting this HttpPostedFile in to byte array.
4.Passing the byte array to database using commandName.Parameter.addWithValue.


这篇关于使用c#在mysql数据库中插入doc文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆