问题描述:

I'm trying to put a base64 String in the SqlServer on Image type or VarBinary(MAX) on Sqlserver's Image type.

for this I tryied to use

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

i tried to convert to memoryStrem and get the Buffer.

to use the type System.Drawing.Image

I tried with bitmap.

but always gives me an exception.

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;

}

}

The exception :

{System.Data.SqlClient.SqlException (0x80131904): String or binary data would be truncated.

The statement has been terminated.

em System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)

em System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean 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, String resetOptionsString)

em System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)

em System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)

em System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean 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

网友答案:

The error means that the text in one of the boxes is longer than the declared column. Either expand the column in the database, or validate the length of the text in the boxes before sending to the server.

相关阅读:
Top