我如何在数据库上载文件 [英] how do i upload files on database
问题描述
如何使用c#Windows应用程序将文件(任何格式)从本地磁盘(硬盘)上载到数据库(sql server 2005)中.
我正在使用Visual Studio 2008和C#Windows应用程序.
请向我发送代码,以从本地硬盘上载大量文件,并使用过程将其保存到sql服务器中.
how do i upload files(any format) into my database(sql server 2005) from my local disc(hard disk) by using c# windows application.
I am using visual studio 2008, c# windows application.
please send me the code to upload a bulk of files from my local hd and save it into sql server using procedures.
推荐答案
以下是将二进制数据插入到其中的代码DB:
Here is the code to insert binary data into DB :
SqlConnection cn=new SqlConnection("put_conn_str");
String query = @"INSERT INTO ImagesStore (OriginalPath,ImageDarta) Values (@path,@data)";
SqlCommand cmd=new SqlCommand(query,cn);
//read file
byte []barray=System.IO.File.ReadAllBytes(dlg.FileName);
//extract the filename only from the selected path
string filename=System.IO.Path.GetFileName(dlg.FileName);
// create sql parameters
cmd.Parameters.Add("@path",SqlDbType.VarChar,100).Value=filename;
cmd.Parameters.Add("@data",SqlDbType.Image,barray.Length).Value=barray;
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
从这里摘录:
http://www.daniweb.com/software-development/csharp/threads/276483 [ ^ ]
当然,路径"是 varchar(100)
,数据"是 image
数据类型.
您也可以使用varbinary
存储二进制数据.
请给我发送代码以上传大量文件"
如果对多个文件重复此操作,则会将多个文件插入数据库.
希望对您有所帮助.
Extracted from here :
http://www.daniweb.com/software-development/csharp/threads/276483[^]
Of course "path" is varchar(100)
and "data" is image
data type.
Also you may use varbinary
for storing binary data.
"please send me the code to upload a bulk of files"
If you repeat this operation for multiple files you will insert multiple files into the DB.
Hope it helps.
批量复制-请参见SqlBulkCopy
命令的实现此处 [ ^ ].
尝试 ^ ]也是如此.
看看此 [
Bulk copy - see an implementation of theSqlBulkCopy
command here[^].
Try this[^] too.
Have a look at this[^] as well.
这篇关于我如何在数据库上载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!