从 DataGridViewLinkColumn 下载文件,作为字节数据存储在 SQL 数据库 - Windows 窗体



我正在制作一个Windows表单应用程序,我使用OpenFileDialog将我的数据存储在SQL数据库中作为字节。我已经包含了代码 - 填充数据显示数据在DataGridViewLink列上,button2_click(浏览和保存按钮(函数将其存储在SQL数据库中。

private void button2_Click(object sender, EventArgs e)
{
DialogResult res = openFileDialog1.ShowDialog();
if (res == DialogResult.OK)
{
FileInfo fi = new FileInfo(openFileDialog1.FileName);
byte[] documentContent = File.ReadAllBytes(openFileDialog1.FileName);
string name = fi.Name;
string extn = fi.Extension;
using (SqlConnection cn = new SqlConnection(LOC))
{
SqlCommand cmd = new SqlCommand("SaveDocument", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = name;
cmd.Parameters.Add("@Content", SqlDbType.VarBinary).Value = documentContent;
cmd.Parameters.Add("@Extn", SqlDbType.VarChar).Value = extn;
cmd.Parameters.Add("@Year", SqlDbType.Int).Value = 2018;
cn.Open();
cmd.ExecuteNonQuery();
}
FillData();
}
}
private void FillData()
{
DataTable dt = new DataTable();
using (SqlConnection cn = new SqlConnection(LOC))
{
SqlCommand cmd = new SqlCommand("GetDocuments", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Year", SqlDbType.Int).Value = "2018";
cn.Open();
SqlDataReader reader = cmd.ExecuteReader();
dt.Load(reader);
}
if(dt.Rows.Count > 0)
{
dataGridView1.DataSource = dt;
foreach (DataGridViewColumn colu in dataGridView1.Columns)
{
if (colu.HeaderText != "Name")
colu.Visible = false;
}
}
}
private void ITDec2018_Load(object sender, EventArgs e)
{
DataGridViewLinkColumn col = new DataGridViewLinkColumn();
col.DataPropertyName = "DName";
col.Name = "Name";
dataGridView1.Columns.Add(col);
FillData();
}

F.Y.I:我只为名称创建自己的表列并隐藏其他列。DName 是数据库中文件的名称

我想单击文件的链接,这只是带有链接的名称,程序应该下载与文件ID关联的文件并将其存储在下载中,或者打开对话框并询问我在哪里存储它,我已经包含了运行应用程序时显示的屏幕截图, 以及 SQL 数据库表。

SQL 数据库

具有数据网格视图链接列的窗体

那么我应该如何做所要求的,我应该更新单元格content_click功能,如果是这样,如何?

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}

任何帮助,不胜感激。

我建议您将ID也添加到datagridview中并使用此代码,方法是替换连接并将SQL查询固定为数据库名称

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
string id = dgv.Rows[e.RowIndex].Cells[0].Value.ToString();
string sqlQuery = string.Format("SELECT DocumentContent,DBName FROM myDatabase WHERE ID={0};", id);
SqlDataAdapter myAdapter1 = new SqlDataAdapter(sqlQuery, myConnection);
DataTable dt = new DataTable();
myAdapter1.Fill(dt);
Stream myStream;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 1;
saveFileDialog1.RestoreDirectory = true;

foreach (DataRow row in dt.Rows)
{
byte[] byteArray = (byte[])row["DocumentContent"];
string name = (string)row["DBName"];
saveFileDialog1.FileName = name;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((myStream = saveFileDialog1.OpenFile()) != null)
{
myStream.Write(byteArray, 0, byteArray.Length);
myStream.Close();
}
}
//File.WriteAllBytes("Path\" + name, byteArray);
}
}

最新更新