使用 C# 在客户端计算机和服务器计算机数据库之间传输文件

发布于 2024-10-09 15:17:13 字数 148 浏览 2 评论 0原文

我在网络中的服务器计算机上有这个 SQL 数据库,我希望任何客户端计算机都能够相应地向数据库上传和下载文件,其中文件将保存到服务器计算机文件系统及其元数据中将存储在数据库中。
执行该任务的最佳方法是什么?我该如何使用 C# winform 应用程序来实现它? 提前谢谢@_@

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.
What is the best method to perform that task? and how shall I implement it using C# winform application?
thx in advance @_@

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哎呦我呸! 2024-10-16 15:17:13

为什么要将文件存储在数据库中?文件系统(我确信相关服务器也有)更适合保存和提供文件。

如果文件必须位于数据库中,那么您可能只想将它们存储在 blob 字段中。当然,还应该将其他字段添加到表中,其中包含有关文件的数据。文件名、文件类型,如果您不想动态地从 blob 中计算文件大小,可能还有上传文件的用户、上传时间等。

此时您将与它就像应用程序中的任何其他数据库记录一样(为简洁起见,跳过对此的解释,不乏有关该主题的教程),并具有在 Blob 数据的字节数组之间进行转换的附加步骤。此外,不乏示例

但我必须回到我最初的问题。为什么要把文件放在关系数据库中?

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. 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.

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. Also, no shortage of examples.

But I have to drive back to my original question. Why put files in a relational database?

捎一片雪花 2024-10-16 15:17:13
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();
}
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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文