编写一个c#窗体,实现在窗体上可以浏览数据库上的文件,并且在窗体上可以对这些文件进行上传或者下载等操作
时间: 2024-04-07 14:33:23 浏览: 16
以下是一个简单的 C# 窗体示例,实现了浏览数据库中文件、上传文件和下载文件的功能。请确保你已经安装了 System.Data.SqlClient 和 System.IO 命名空间。
```csharp
using System;
using System.Data.SqlClient;
using System.IO;
using System.Windows.Forms;
namespace FileBrowser
{
public partial class MainForm : Form
{
// 数据库连接字符串
private string connectionString = "Data Source=服务器名称;Initial Catalog=数据库名称;User ID=用户名;Password=密码";
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
LoadFilesFromDatabase();
}
// 从数据库加载文件列表
private void LoadFilesFromDatabase()
{
// 清空文件列表
listBoxFiles.Items.Clear();
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
// 打开数据库连接
connection.Open();
// 查询数据库中的文件列表
string sql = "SELECT FileName FROM Files";
SqlCommand command = new SqlCommand(sql, connection);
SqlDataReader reader = command.ExecuteReader();
// 将文件名添加到列表框中
while (reader.Read())
{
string fileName = reader.GetString(0);
listBoxFiles.Items.Add(fileName);
}
// 关闭数据阅读器和数据库连接
reader.Close();
connection.Close();
}
}
catch (Exception ex)
{
MessageBox.Show("加载文件列表失败:" + ex.Message);
}
}
// 上传文件到数据库
private void btnUpload_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string fileName = openFileDialog.FileName;
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
// 打开数据库连接
connection.Open();
// 读取文件内容
byte[] fileContent = File.ReadAllBytes(fileName);
// 插入文件到数据库
string sql = "INSERT INTO Files (FileName, FileContent) VALUES (@FileName, @FileContent)";
SqlCommand command = new SqlCommand(sql, connection);
command.Parameters.AddWithValue("@FileName", Path.GetFileName(fileName));
command.Parameters.AddWithValue("@FileContent", fileContent);
command.ExecuteNonQuery();
// 关闭数据库连接
connection.Close();
// 重新加载文件列表
LoadFilesFromDatabase();
}
}
catch (Exception ex)
{
MessageBox.Show("上传文件失败:" + ex.Message);
}
}
}
// 下载选定的文件
private void btnDownload_Click(object sender, EventArgs e)
{
if (listBoxFiles.SelectedItem == null)
{
MessageBox.Show("请先选择要下载的文件");
return;
}
string fileName = listBoxFiles.SelectedItem.ToString();
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
// 打开数据库连接
connection.Open();
// 查询文件内容
string sql = "SELECT FileContent FROM Files WHERE FileName = @FileName";
SqlCommand command = new SqlCommand(sql, connection);
command.Parameters.AddWithValue("@FileName", fileName);
byte[] fileContent = (byte[])command.ExecuteScalar();
// 保存文件到本地
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.FileName = fileName;
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
File.WriteAllBytes(saveFileDialog.FileName, fileContent);
MessageBox.Show("文件下载成功");
}
}
}
catch (Exception ex)
{
MessageBox.Show("下载文件失败:" + ex.Message);
}
}
}
}
```
在这个示例中,我们使用了一个 `listBoxFiles` 列表框来显示数据库中的文件列表。`btnUpload` 按钮用于上传文件到数据库,`btnDownload` 按钮用于下载选定的文件。
在 `LoadFilesFromDatabase` 方法中,我们从数据库中查询文件列表,并将文件名添加到 `listBoxFiles` 列表框中。
在 `btnUpload_Click` 方法中,我们使用 `OpenFileDialog` 对话框选择要上传的文件。然后,我们读取文件的内容,并将文件和文件名插入到数据库中。
在 `btnDownload_Click` 方法中,我们首先检查用户是否选择了要下载的文件。然后,我们从数据库中查询选定文件的内容,并使用 `SaveFileDialog` 对话框指定下载文件的保存路径。
请记得根据你的实际情况修改数据库连接字符串和数据库表结构等信息。