从DataGridViewLinkColumn下载文件以字节数据形式存储在SQL数据库中-Windows Form Application

时间:2018-07-05 10:34:59

标签: c# sql datagridview windows-forms-designer

我正在制作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 Database

Form with datagridViewLink Column

那我该怎么做,该如何更新单元格content_click函数,如果是,该如何更新?

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

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我建议您通过替换连接并将SQL查询固定为数据库名称

,将ID也添加到datagridview中并使用此代码。
    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);
        }

    }