Click here to Skip to main content
15,441,149 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Good day, over a month ago I came looking for all sides and can not find anything, I save an image on a MYSQL database to a web application with Web Reference

WebMethod(Description = "Inserta Imagen en Tabla Clientes")]
public int SaveImage(byte[] buffer, string rrdbcoim)
{    
   string strcmd = "INSERT INTO imagen (im_dblogo, rr_dbcoim) VALUES (@logo, @codi)";    //use the web.config to store the connection string    

    MySqlConnection conexion = new MySqlConnection(DSN1);       
    conexion.Open();    

    MySqlCommand cmd = new MySqlCommand(strcmd, conexion);      
    MySqlParameter param = new MySqlParameter("@logo", MySqlDbType.LongBlob);    
    param.Size = buffer.Length;    
    param.Value = buffer;    

    cmd.Parameters.Add(param);    
    cmd.Parameters.Add(new MySqlParameter("@codi", rrdbcoim));    

    conexion.Open();    

    int numRowsAffected = cmd.ExecuteNonQuery();    
    conexion.Close();    

    return numRowsAffected;
}


I want to push a button to accept me save the image.
also use AsyncFileUpload. I need urgent help
Posted
Updated 22-Apr-10 10:45am
v2

I'm lost - where is the issue ? Does this code work ? What bit is your problem ? Is it calling a webservice method, or does this code just not work ?
 
Share this answer
 
use this ? instead of @

WebMethod(Description = "Inserta Imagen en Tabla Clientes")]
public int SaveImage(byte[] buffer, string rrdbcoim)
{
string strcmd = "INSERT INTO imagen (im_dblogo, rr_dbcoim) VALUES (?logo, ?codi)"; //use the web.config to store the connection string

MySqlConnection conexion = new MySqlConnection(DSN1);
conexion.Open();

MySqlCommand cmd = new MySqlCommand(strcmd, conexion);
MySqlParameter param = new MySqlParameter("?logo", MySqlDbType.LongBlob);
param.Size = buffer.Length;
param.Value = buffer;

cmd.Parameters.Add(param);
cmd.Parameters.Add(new MySqlParameter("?codi", rrdbcoim));

conexion.Open();

int numRowsAffected = cmd.ExecuteNonQuery();
conexion.Close();

return numRowsAffected;
}
 
Share this answer
 
/// <summary>
/// Added by VRaj, to read an image file and return as MySQL LongBlob datatype parameter
/// </summary>
/// <param name="filePath">Image file path</param>
/// <param name="parameterName">Sql Parameter name to pass in Query, Prefix with '?'</param>
/// <returns>MySql Parameter with LongBlob datatype</returns>
public MySqlParameter ReadImageFile(string filePath, string parameterName)
{
BinaryReader br = null;
try
{
if (filePath.Trim() != "" && filePath.Length > 0 && parameterName.Trim() != "" && parameterName.Length > 0)
{
if (parameterName.Contains("?")) //Checks if a valid Sql Parameter name is sent in parameterName
{
if (File.Exists(filePath)) //Checks if File exists in the path
{
#region Load image into byte[]
byte[] data = null;
FileInfo fInfo = new FileInfo(filePath);
long numBytes = fInfo.Length;

using (FileStream fStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
br = new BinaryReader(fStream);
data = br.ReadBytes((int)numBytes);
}
#endregion

#region Assign byte[] image into a MySqlParameter
MySqlParameter param = new MySqlParameter(parameterName, MySqlDbType.LongBlob);
param.Size = data.Length;
param.Value = data;
#endregion

br.Close();
return param;
}
else
return null;
}
else
return null;
}
else
return null;
}
catch (Exception)
{
br.Close();
return null;
}
#region Sample code to use this parameter column in queries - Do Not Delete
/*MySqlParameter FS = cm.ReadImageFile(@"D:/GulfEMR2008/Sign/110Anesth1.gif", "?logo");
string strcmd = "Update tblevaldetails set anesthplandatetime='2010-06-30 00:00:00', EBL = ?logo where detailid=124";
MySqlConnection conexion = new MySqlConnection(ConfigurationSettings.AppSettings["SQLConnection"].ToString());
MySqlCommand cmd = new MySqlCommand(strcmd, conexion);
cmd.Parameters.Add(FS);
conexion.Open();
cmd.ExecuteNonQuery();
conexion.Close();*/
#endregion
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900