0

我正在尝试将 base64 字符串放入 Image 类型的 SqlServer 或 Sqlserver 的 Image 类型的 VarBinary(MAX) 中。为此我尝试使用

byte[] b = Convert.FromBase64String(mybase64); and put it as a parameter;

我试图转换为 memoryStrem 并获取缓冲区。

使用 System.Drawing.Image 类型

我尝试使用位图。

但总是给我一个例外。

public bool Insert(List<Arquivo> arquivos, String idponto)
    {

        List<String> Idimgs = criaIdImg(idponto, arquivos.Count);
        for (int i = 0; i<arquivos.Count; i++ )
        {
            cmdInsert.Parameters.AddWithValue("@IdImagem", Idimgs[i]);
            cmdInsert.Parameters.AddWithValue("@IdPonto", idponto);
            cmdInsert.Parameters.AddWithValue("@NomeImagem", arquivos[i].arquivo);
            //System.Convert.FromBase64String(arquivos[i].arquivo)
            Image img = this.Base64ToImage(arquivos[i].arquivo);
            System.IO.MemoryStream stream = new System.IO.MemoryStream();
            img.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);

            int tamanho = Convert.ToInt32(stream.Length);
            //byte[] blob = new byte[tamanho];
           // stream.Read(blob, 0, tamanho);

            cmdInsert.Parameters.AddWithValue("@IMG", stream.GetBuffer());
            cmdInsert.ExecuteNonQuery();
            cmdInsert.Parameters.Clear();
        }
        return true;
    }

    public List<String> criaIdImg(String idPonto, int qtdArquivos){
        List<String> ids = new List<String>();
        String max = idPonto;
        int n = 0;

        SqlDataAdapter daTipo = new SqlDataAdapter(cmdId);
        cmdId.Parameters.AddWithValue("@IDPONTO", idPonto);


        daTipo.Fill(dataTable);

        foreach (DataRow dRow in dataTable.Rows)
        {
            String num = (String)dRow[0];
            num = num.Substring(7);
            n = Convert.ToInt32(num);
            n++;
        }

        for (int i = 0; i < qtdArquivos; i++)
        {
            if(n<10){ 
            ids.Add(idPonto + "00" +n);
            }
            else if (n < 100)
            {
                ids.Add(idPonto + "0" + n);
            }
            else
            {
                ids.Add(idPonto + n);
            }
            n++;
        }
        return ids;
    }

    public Image Base64ToImage(string base64String)
    {
        // Convert Base64 String to byte[]
        byte[] imageBytes = Convert.FromBase64String(base64String);
        MemoryStream ms = new MemoryStream(imageBytes, 0,
          imageBytes.Length);

        // Convert byte[] to Image
        ms.Write(imageBytes, 0, imageBytes.Length);
        Image image = Image.FromStream(ms, true);
        return image;
    }

}

例外:

{System.Data.SqlClient.SqlException (0x80131904):字符串或二进制数据将被截断。
该语句已终止。
   em System.Data.SqlClient.SqlConnection.OnError(SqlException 异常,布尔型 breakConnection)
   em System.Data.SqlClient.SqlInternalConnection.OnError(SqlException 异常,布尔型 breakConnection)
   em System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
   em System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior,SqlCommand cmdHandler,SqlDataReader dataStream,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj)
   em System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,RunBehavior runBehavior,字符串 resetOptionsString)
   em System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior,RunBehavior runBehavior,布尔返回流,布尔异步)
   em System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String 方法, DbAsyncResult 结果)
   em System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult 结果,字符串方法名,布尔型 sendToPipe)
   em System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
   em Caderneta.ArquivosDAO.Insert(List`1 arquivos, String idponto) na C:\Users\leandro\Documents\Visual Studio 2010\Projects\Caderneta\Caderneta\ArquivosDAO.cs:linha 61
   em Caderneta.Sincronizar.TesteGravaImg(String img1, String img2, String idponto) na C:\Users\leandro\Documents\Visual Studio 2010\Projects\Caderneta\Caderneta\Sincronizar
4

1 回答 1

1

该错误意味着其中一个框中的文本比声明的列长。要么展开数据库中的列,要么在发送到服务器之前验证框中文本的长度。

于 2013-06-13T12:33:36.857 回答