【问题标题】:Insert byte[] to blob column in Informix DB using c#使用 c# 将字节 [] 插入 Informix DB 中的 blob 列
【发布时间】:2013-05-23 09:32:02
【问题描述】:

就像这些链接

Link 1

Link 2

Link 3

Link 4

我也无法在我的 informix 数据库上仅插入 byte[] 相关操作。我尝试了很多方法并浏览了 IBM 网站。但没有解释“如何使用 byte[] 使用 c# 插入 blob 列”。

“LINK 4”非常有用。但我面临着这段代码的问题。

Error: The %0 enumeration value, %1, is invalid.
At line: blob.Open(IfxSmartLOBOpenMode.ReadWrite);
if i use cmd.Parameters.Add(new IfxParameter()).Value = byteuploaded`;

这是我的代码 sn-p。

protected void uploadfile_Click(object sender, EventArgs e)
    {
        string extension;
        // checks if file exists
        if (!_imageUpload.HasFile)
        {
            _resultLbl.Text = "Please, Select a File!";
            return;
        }
        // checks file extension
        extension = System.IO.Path.GetExtension(_imageUpload.FileName).ToLower();
        if (!extension.Equals(".jpg") && !extension.Equals(".jpeg") && !extension.Equals(".png"))
        {
            _resultLbl.Text = "Only image files (.JPGs and .PNGs) are allowed.";
            return;
        }
        try
        {
            // =========   This is not working   ==============
            string sqlQuery = "insert into db95:TestBlobUpload (id ,fileblob) values('2', 'two');";
            // =========   This is working properly  ==============
            //string sqlQuery = "insert into db95:TestBlobUpload (id ,filetext) values('4',?);";
            string connString = "Database=db95;Host=172.16.XX.XX;Server=vsXXXX;Service=88;Protocol=onsoctcp;UID=ed;Password=ca94;";
            using (this.connection = new IfxConnection(connString))
            {
                this.connection.Open();
                using (this.cmd = new IfxCommand(sqlQuery, this.connection))
                {
                    // Start a local transaction
                    this.trans = this.connection.BeginTransaction(IsolationLevel.Unspecified);
                    // Assign transaction object for a pending local transaction
                    this.cmd.Transaction = trans;
                    try
                    {


                        IfxBlob byteuploaded = new IfxBlob(this.connection);
                        byteuploaded.Read(_imageUpload.FileBytes);

                        // =========   BOTH OF THESE are not working   ==============
                        //cmd.Parameters.Add(new IfxParameter()).Value = data;// System.Text.Encoding.UTF8.GetString(data);
                        cmd.Parameters.Add(new IfxParameter()).Value = byteuploaded;// _imageUpload.FileBytes;

                        int res = cmd.ExecuteNonQuery();

                        // commiting the transaction
                        this.cmd.Transaction.Commit();
                    }
                    catch
                    {
                        //this.cmd.Transaction.Rollback();
                    }
                }
                this.connection.Close();
            }
        }
        catch (Exception)
        {

        }
    }

我使用这个 dll 作为参考并使用 IBM.Data.Informix;

尤其是无法将 byte[] 添加到 blob 列。我可以做的所有其他插入/更新/删除操作。

有什么帮助吗?

我什至升级到 ibm_data_server_driver_package_win64_v10.1.exe & clientsdk.4.10.FC1DE.WIN.exe

但是我面临着 dll 兼容性的问题。无法加载“XX.XX.dll”异常来了。

我什至尝试使用

执行插入查询
INSERT INTO db95@vsXXXX:testblobupload (fileblob) 
   VALUES (db95@vsXXXX:FILETOBLOB('C:\tmp\Untitled.png', 'client'));

并面临错误

ERROR: Smart-large-object error.
Error Code: -9810.
Smart Large Objects: No sbspace number specified.

【问题讨论】:

  • 你成功了吗?我也有同样的问题

标签: c# asp.net image blob informix


【解决方案1】:

这不是您的 c# 应用程序。需要为大型智能对象设置 Informix 环境。我认为它基本上指定了在服务器上使用的空间。我不知道别的。如果您返回 isam 错误代码,它将是这样的

-12053 智能大对象:未指定 sbspace 编号。

未找到默认 sbspace,并且调用方未指定 sbspace 使用。

在 智能大对象函数调用或设置 SBSPACENAME onconfig 文件参数添加到有效的智能大对象空间的名称。

【讨论】:

    【解决方案2】:

    我们可以使用 PUT IN 子句在特定 sbspace 中包含 blob 字段。

    如果我们在不指定表名和列名的情况下使用connection.GetIfxBlob(),则blob字段将包含在onconfigSBSPACENAME中设置的默认sbspace中,如果不设置则会报错SQLERR-9810 ISAM ERR-12053

    【讨论】:

      【解决方案3】:

      我猜,保存 Informix Blob 的最佳方法是使用此函数:

          string insertSql = string.Format("insert into \"{0}\" (sbfotoint,NmArqBlob) values (?,?);", this.table);
      
              using (var command = new IfxCommand(insertSql, this.connection))
              {
                  this.connection.Open();
                  SetRole();
                  command.CommandType = System.Data.CommandType.Text;
                  command.Parameters.Add(new IfxParameter()).Value = CreateIfxBlob(entidade.SBlob);
                  command.Parameters.Add(new IfxParameter()).Value = entidade.NomeArquivo;
                  command.ExecuteNonQuery();
                  this.connection.Close();
              }
      
      private IfxBlob CreateIfxBlob(byte[] data)
          {
              IfxBlob blob = connection.GetIfxBlob(this.table, "sbfotoint");
              blob.Open(IfxSmartLOBOpenMode.ReadWrite);
              blob.Write(data);
              blob.Close();
              return blob;
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-05-24
        • 1970-01-01
        • 1970-01-01
        • 2012-06-24
        • 1970-01-01
        • 2013-11-02
        • 2011-09-10
        • 1970-01-01
        相关资源
        最近更新 更多