如何使用C#中的存储过程将docs-pdf图像文件存储在数据库中

本文关键字:文件 图像 存储 docs-pdf 数据库 何使用 存储过程 | 更新日期: 2023-09-27 18:20:15

我已经编写了以下代码,使用c#.net将pdf/docs/image文件存储在数据库中和sqlserver2008存储过程,但在转换了字节数组中的文件后,我无法做到这一点。

代码:

 private void bttnSave_Click(object sender, EventArgs e)
        {
            FrmAccept sForm = new FrmAccept();
            sForm.ShowDialog();
            if (FrmAccept.strMsg == "YES")
            {
                sFunctionQuotationSave();
                MessageBox.Show("Data Saved Successfully", "Insert Record", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            { return; }
        }
        public void sFunctionQuotationSave()
        {
            SERIALNUMBER = 1;
            Int32 Id = Convert.ToInt32(sSearch);
            String bytestring = null;
            for (int i = 0; i < dgvQuotationVouchers.Rows.Count; i++)
            {
                string filename = dgvQuotationVouchers.Rows[i].Cells[5].Value.ToString();
                MessageBox.Show("FIlePAth>>" + filename);

                byte[] bytes = File.ReadAllBytes(dgvQuotationVouchers.Rows[i].Cells[5].Value.ToString());
                SqlParameter fileP = new SqlParameter("Upload", SqlDbType.VarBinary);
                fileP.Value = bytes;
                SqlCommand myCommand = new SqlCommand();

                SqlCommand sqlcomm = new SqlCommand();
                sqlcomm.Parameters.Add(new SqlParameter("@Upload", SqlDbType.VarBinary)).Value = bytes;
                dt = sFunctions.ExecuteQuery(Variables.con, "Quotation_Master", "usp_Quotation_Master " + Id + ",'" + dgvQuotationVouchers.Rows[i].Cells[1].Value + "'," + Variables.sTendercode + "," + txtIndentNo.Text + ",'" + dgvQuotationVouchers.Rows[i].Cells[3].Value + "','" + dgvQuotationVouchers.Rows[i].Cells[4].Value + "','" + dgvQuotationVouchers.Rows[i].Cells[1].Value + "','" + dgvQuotationVouchers.Rows[i].Cells[5].Value + "'," + + ",'" + Variables.LocationId + "'," + false + ",'" + Variables.Layer + "'," + rbtnLocal.Checked + "," + rbtnGlobal.Checked + "," + false + "");
                }
            }
             MessageBox.Show("BYTE_DATA>>'n"+bytestring);
       }

如何使用C#中的存储过程将docs-pdf图像文件存储在数据库中

您的代码中有2个SqlCommand对象。。并且没有设置命令文本属性。。

你应该有这样的东西:

SqlParameter fileP = new SqlParameter("@Upload", SqlDbType.VarBinary);
fileP.Value = bytes; //Here, you're assigning the value to the parameter.
SqlCommand myCommand = new SqlCommand();
myCommand.Connection = someSqlConnection;
myCommand.CommandText = "YOUR UPDATE STATEMENT";
myCommand.Parameters.Add(fileP);
myCommand.ExecuteNonQuery();