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

时间:2014-09-01 11:31:58

标签: c# sql-server-2008 stored-procedures

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

代码:

 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);
       }

1 个答案:

答案 0 :(得分:0)

您的代码中有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();