[英]file transfer between a client machine and a server machine database using C#
I have this SQL database on a server machine in a network and I want any of the client machines to be able to upload and download files to and from the database accordingly where the files will be saved to the server machine file system and their meta data will be stored in the database. 我在网络中的服务器计算机上拥有此SQL数据库,并且我希望任何客户端计算机都能够相应地向数据库上载文件或从数据库中下载文件,在这些文件中文件将保存到服务器计算机文件系统及其元数据中将存储在数据库中。
What is the best method to perform that task? 执行该任务的最佳方法是什么? and how shall I implement it using C# winform application? 以及如何使用C#winform应用程序实现它? thx in advance @_@ 提前谢谢@ _ @
try
{
if (txtFilePath.Text == pdfFilepath )
{
byte [] b=File .ReadAllBytes (pdfFilepath);
SqlConnection con1=Connection.Conn();
SqlCommand cmd=new SqlCommand ("insert into tbl_Document(Company_Id,FileNo,PdfFile)values(@cid,@fno,@file)",con1);
cmd .Parameters .AddWithValue("@cid",txtCompanyId.Text);
cmd .Parameters .AddWithValue("@fno",txtFileNumber.Text);
cmd .Parameters .AddWithValue("@file",b);
con1.Open();
cmd .ExecuteNonQuery();
con1.Close();
}
else
{
MessageBox.Show("Please give a valid file address.");
this.Dispose();
}
}
catch
{
MessageBox.Show("Unable to Process.");
this.Dispose();
}
Why do you want the files to be stored in a database? 为什么要将文件存储在数据库中? A file system (which I'm sure the server in question also has) is much more suited to keeping and serving files. 文件系统(我确定所涉及的服务器也具有该文件系统)更适合保留和提供文件。
If the files must be in the database, then you'd probably just want to store them in a blob field. 如果文件必须在数据库中,那么您可能只想将它们存储在blob字段中。 Additional fields should be added to the table with data about the files, of course. 当然,应该将其他字段与有关文件的数据一起添加到表中。 File name, file type, maybe file size if you don't want to have to calculate it from the blob on the fly, maybe the user who uploaded it, the time it was uploaded, etc. 文件名,文件类型,文件大小(如果您不想从运行中的Blob中进行计算),文件的上传用户,文件的上传时间等。
At that point you'd interact with it just like any other database record in your application (skipping explanation of that for brevity, there's no shortage of tutorials on the subject), with the additional step of converting to and from byte arrays for the blob data. 到那时,您将与应用程序中的任何其他数据库记录进行交互(为简洁起见,跳过该说明,关于该主题的教程不乏),另外还包括为blob进行字节数组转换的额外步骤。数据。 Also, no shortage of examples . 另外, 不乏例子 。
But I have to drive back to my original question. 但是我必须回到我原来的问题。 Why put files in a relational database? 为什么要将文件放在关系数据库中?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.