尝试在Oracle数据库中保存图像

时间:2017-12-05 09:58:54

标签: c# asp.net oracle

我正在尝试检查图像是否可以保存在oracle db上,但是卡在一点,错误是“无效的表名”我试图调试并找到错误但是无法确定问题的原因发生。我创建的表是Par_Image是非常基本和简单的,在dotnet方面我对下面的代码有一些疑问

insertCommand.Parameters.Add(“Picture”,OracleType.Blob,0).Value

这里有OracleType.Blob。它应该是OracleDBType.Blob,但OracleDbType不可用,所以我使用了OracleType.Blob,但问题仍然存在。试图找出错误,但不知道如何继续。也许问题会出现在我的代码或方法中?如果有任何其他方式,请告诉我。

Oracle DB

create table Par_Image
(
  PRODUCT_ID    NUMBER(10) not null,
  Picture       BLOB  not null 
)

的.aspx

<form id="form1" runat="server">
        <div>
            <asp:FileUpload runat="server" ID="flImage" />
            <hr />
            <asp:Button runat="server" ValidationGroup="Details" ID="btnSubmit" Text="Upload" OnClick="btnSubmit_Click" />
        </div>
        <asp:Label ID="lblSuccess" runat="server" ForeColor="Green" />
    </form>

C#side

  private byte[] ConvertImageToByteArray(System.Drawing.Image imageToConvert,
                                           System.Drawing.Imaging.ImageFormat formatOfImage)
        {
            byte[] Ret;
            try
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    imageToConvert.Save(ms, formatOfImage);
                    Ret = ms.ToArray();
                }
            }
            catch (Exception) { throw; }
            return Ret;
        }




    protected void btnSubmit_Click(object sender, EventArgs e)
        {
                           System.Drawing.Image imag = System.Drawing.Image.FromStream(flImage.PostedFile.InputStream);
            System.Data.OracleClient.OracleConnection conn = null;
            try
            {
                try
                {
                    conn = new System.Data.OracleClient.OracleConnection(
                        ConfigurationManager.ConnectionStrings["conndbprodnew"].ConnectionString);
                    conn.Open();
                    System.Data.OracleClient.OracleCommand insertCommand =
                        new System.Data.OracleClient.OracleCommand(
                        "Insert into [Par_Image] (PRODUCT_ID, Picture) Values (1, :Picture)", conn);
                    insertCommand.Parameters.Add("Picture",OracleType.Blob, 0).Value =
                        ConvertImageToByteArray(imag, System.Drawing.Imaging.ImageFormat.Jpeg);
                    int queryResult = insertCommand.ExecuteNonQuery();
                    if (queryResult == 1)
                        lblSuccess.Text = "msg record Created Successfully";
                }
                catch (Exception ex)
                {
                    lblSuccess.Text = "Error: " + ex.Message;
                }
            }
            finally
            {
                if (conn != null)
                    conn.Close();
            }
        }

0 个答案:

没有答案