使用ASP.NET C将图像保存在MySQL DB中 [英] Save Image in MySQL DB using ASP.NET C #

查看:81
本文介绍了使用ASP.NET C将图像保存在MySQL DB中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天,一个多月前,我到处寻找东西,却找不到任何东西,我使用Web参考将图像保存在MYSQL数据库中到Web应用程序中

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;
}



我想按一个按钮接受我保存图像.
还可以使用AsyncFileUpload.我需要紧急帮助



I want to push a button to accept me save the image.
also use AsyncFileUpload. I need urgent help

推荐答案

我迷路了-问题出在哪里?此代码有效吗?你有什么问题?它是在调用Web服务方法,还是这段代码不起作用?
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 ?


使用此方法?而不是@

WebMethod(描述=在Tabla客户端中插入Inserta图像")]
public int SaveImage(byte [] buffer,字符串rrdbcoim)
{
字符串strcmd =插入图像(im_dblogo,rr_dbcoim)值(?logo,?codi)" //使用web.config存储连接字符串

MySqlConnection conexion =新的MySqlConnection(DSN1);
conexion.Open();

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

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

conexion.Open();

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

返回numRowsAffected;
}
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;
}


///& lt; summary& gt;
///由VRaj添加,用于读取图像文件并作为MySQL LongBlob数据类型参数返回
///& lt;/summary& gt;
///& lt; param name ="filePath"& gt;图片文件路径& lt;/param& gt;
///< param name ="parameterName"& gt; Sql要在查询中传递的参数名称,前缀带有?"& lt;/param& gt;
///< returns& gt;具有LongBlob数据类型的MySql参数</returns& gt;
公共MySqlParameter ReadImageFile(字符串filePath,字符串parameterName)
{
BinaryReader br = null;
试试
{
如果(filePath.Trim()!="& amp; amp; filePath.Length& gt; 0& amp; amp; amp; parameterName.Trim()!="& amp;& amp; parameterName.Length& gt; 0)
{
if(parameterName.Contains(?"))//检查是否在parameterName中发送了有效的Sql参数名称 {
if(File.Exists(filePath))//检查路径中是否存在File
{
#region将图片加载到byte []
byte [] data = null;
FileInfo fInfo =新的FileInfo(filePath);
long numBytes = fInfo.Length;

使用(FileStream fStream = new FileStream(filePath,FileMode.Open,FileAccess.Read))
{
br = new BinaryReader(fStream);
数据= br.ReadBytes((int)numBytes);
}
#endregion

#region将byte []图像分配到MySqlParameter
MySqlParameter param = new MySqlParameter(parameterName,MySqlDbType.LongBlob);
param.Size = data.Length;
param.Value =数据;
#endregion

br.Close();
返回参数;
}
其他
返回null;
}
其他
返回null;
}
其他
返回null;
}
catch(Exception)
{
br.Close();
返回null;
}
#region在查询中使用此参数列的示例代码-请勿删除
/* 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 =新的MySqlConnection(ConfigurationSettings.AppSettings ["SQLConnection"].ToString());
MySqlCommand cmd =新的MySqlCommand(strcmd,conexion);
cmd.Parameters.Add(FS);
conexion.Open();
cmd.ExecuteNonQuery();
conexion.Close(); */
#endregion
}
/// &lt;summary&gt;
/// Added by VRaj, to read an image file and return as MySQL LongBlob datatype parameter
/// &lt;/summary&gt;
/// &lt;param name="filePath"&gt;Image file path&lt;/param&gt;
/// &lt;param name="parameterName"&gt;Sql Parameter name to pass in Query, Prefix with ''?''&lt;/param&gt;
/// &lt;returns&gt;MySql Parameter with LongBlob datatype&lt;/returns&gt;
public MySqlParameter ReadImageFile(string filePath, string parameterName)
{
BinaryReader br = null;
try
{
if (filePath.Trim() != "" &amp;&amp; filePath.Length &gt; 0 &amp;&amp; parameterName.Trim() != "" &amp;&amp; parameterName.Length &gt; 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
}


这篇关于使用ASP.NET C将图像保存在MySQL DB中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆