图片二进制流的方式存入数据库,插入的总是空数据,请高手帮帮我啊~~ 急,在线等

blackscyther 2008-07-28 07:41:27
ASP.net C# MySql数据库 开发 想实现的功能是把图片以二进制流的方式存入数据库,可是程序都和网上的示例一样,程序也能转通,不报错。但是总是没有数据插进去,用Response.Write(SqlCmd);
输出的插入语句为: insert into image(ImageData,ImageContentType,ImageDescription,ImageSize) values(@Image,@ContentType,@ImageDescription,@ImageSize)

值都没有带入变量中

这是为什么呢,期待高人解答~~~


源代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using MySql.Data;
using MySql.Data.MySqlClient;


public partial class test_test2 : System.Web.UI.Page
{
protected Int32 FileLength = 0;

protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
HttpPostedFile UpFile = FileUpload1.PostedFile;
FileLength = UpFile.ContentLength; //记录文件长度
try
{
if (FileLength == 0)
{
this.Label2.Text = "<b>请你选择上传的文件</b>";
}
else
{
Byte[] FileByteArray = new Byte[FileLength]; //图片临时存储的Byte数组
Stream StreamObject = UpFile.InputStream; //建立数据流对象
//读取图像文件数据,FileByteArray为图像存储的实体,0为读取起始位置,FileLength为数据长度
StreamObject.Read(FileByteArray, 0, FileLength);

//建立Sql Server连接
MySqlConnection con = new MySqlConnection("server =localhost;database =bmc;uid=root;pwd = ");
String SqlCmd = "insert into image(ImageData,ImageContentType,ImageDescription,ImageSize) values(@Image,@ContentType,@ImageDescription,@ImageSize)";
MySqlCommand CmdObj = new MySqlCommand(SqlCmd, con);
//设置参数
CmdObj.Parameters.Add("@Image", MySqlDbType.Blob, FileLength); //图片信息
CmdObj.Parameters.Add("@ContentType", MySqlDbType.VarChar,50); //图片类型
CmdObj.Parameters.Add("@ImageDescription", MySqlDbType.VarChar,200);//图片简介
CmdObj.Parameters.Add("@ImageSize", MySqlDbType.Int32); //图片大小

CmdObj.Parameters["@Image"].Value = FileByteArray;
//记录文件类型
CmdObj.Parameters["@ContentType"].Value = UpFile.ContentType;
//其他表单数据记录上传
CmdObj.Parameters["@ImageDescription"].Value = this.TextBox1.Text;
//记录文件长度
CmdObj.Parameters["@ImageSize"].Value = UpFile.ContentLength;

con.Open();
CmdObj.ExecuteNonQuery();
Response.Write(SqlCmd);
con.Close();

this.Label2.Text = "<b>图片上传成功</b>";
}
}
catch (Exception ex)
{
this.Label2.Text = ex.ToString();
}
}
}
...全文
171 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
izee 2008-07-29
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 blackscyther 的回复:]
引用 4 楼 Jinglecat 的回复:
MySql 的语法跟 SQLServer 一样?


MySql的语法跟SQLServer不一样么?  能说得详细点么
[/Quote]
MSSQL除了标准SQL语法外,还有私有语法,是其它SQL不支持的
blackscyther 2008-07-29
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 wangjun8868 的回复:]
LZ啊 MYSQL用的是“?”不是"@"
[/Quote]

正解!! 果然是这样,谢谢~~

看来MySql的语法跟 SQLServer 确实有不一样的地方
guyan033 2008-07-29
  • 打赏
  • 举报
回复
学习了
编程有钱人了 2008-07-29
  • 打赏
  • 举报
回复
LZ啊 MYSQL用的是“?”不是"@"
编程有钱人了 2008-07-29
  • 打赏
  • 举报
回复


//图片二进制读取于存储到数据库

//这里用的是MYSQL数据库的BLOB字段存储的图片二进制数据
//由于用的是MYSQL数据库 所以在引用存储的方法与MSSQL数据库不一样,
MYSQL用的是“?”,而MSSQL数据库是“@”,请大家注意参考
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using System.Drawing.Imaging;
using MySql.Data.MySqlClient;//引用MYSQL命名空间
public partial class _Default : System.Web.UI.Page
{
MySqlConnection conn = new MySqlConnection(System.Configuration.ConfigurationManager.AppSettings["conmy"]);
protected void Page_Load(object sender, EventArgs e)
{
if (!this.Page.IsPostBack)
{
this.show();
}
}

protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
//文件扩展名
string Ex=FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf('.') + 1);
if (Ex.ToLower() == "jpg" || Ex.ToLower() == "gif")
{
int ImageSize = FileUpload1.PostedFile.ContentLength;//图片的大小
string ImageType = this.FileUpload1.PostedFile.ContentType;//图片类型
Stream ImageStream = this.FileUpload1.PostedFile.InputStream;
Byte[] ImageCount = new Byte[ImageSize];//调用方法转化二进制数据
int bt = ImageStream.Read(ImageCount, 0, ImageSize);

MySqlCommand comm = new MySqlCommand("testpic",conn);
comm.CommandType = CommandType.StoredProcedure;
MySqlParameter pj = new MySqlParameter("?myimage", MySqlDbType.LongBlob,ImageCount.Length);//图片
pj.Value =ImageCount;//给这字段赋值二进制数据
comm.Parameters.Add(pj);
MySqlParameter pname = new MySqlParameter("?imagename", MySqlDbType.VarChar, 100);//图片名称
pname.Value=FileUpload1.FileName;
comm.Parameters.Add(pname);
MySqlParameter psize = new MySqlParameter("?imagesize", MySqlDbType.Int32);//图片名称
psize.Value =ImageSize;
comm.Parameters.Add(psize);
conn.Open();
comm.ExecuteNonQuery();
conn.Close();

this.Label1.Text="插入成功!";
this.show();

}
else
{
this.Label1.Text = "格式不正确!!!";
}
}
else
{
this.Label1.Text = "没有文件!!!";
}
}
private byte[] ImageToByte(string path)//声名一个把图片转化到二进制数据的方法
{
FileStream buffer = new FileStream(path, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(buffer);
byte[] image = br.ReadBytes((int)buffer.Length);
return image;
}
private void show()
{
MySqlDataAdapter da = new MySqlDataAdapter("select * from imgpic", conn);
DataSet ds = new DataSet();
da.Fill(ds, "a");
this.GridView1.DataSource = ds.Tables["a"].DefaultView;
this.GridView1.DataKeyNames = new string[] { "id" };
this.GridView1.DataBind();
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
string sid = this.GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();
Session["id"] = sid;
this.Label1.Text = sid;
this.Image1.ImageUrl = "jpg2.aspx";
}
}
读取图片
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;
public partial class Default2 : System.Web.UI.Page
{
MySqlConnection conn = new MySqlConnection(System.Configuration.ConfigurationManager.AppSettings["conmy"]);
protected void Page_Load(object sender, EventArgs e)
{

if (!this.Page.IsPostBack)
{
this.show();
}
}
public void show()
{

string ss = Request.QueryString["id"].ToString();
string s3 = "select * from imgpic where id=" + ss;
MySqlCommand comm = new MySqlCommand(s3, conn);
conn.Open();
MySqlDataReader dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
while(dr.Read())
{
Response.Clear();
Response.C;
Response.BinaryWrite((byte[])dr["myimg"]);//读取
}
Response.End();
conn.Close();
}
}
//注意图片显示要在另一张页面中
//在你要显示的页面添加img 控件src="显示你图片的页面";

blackscyther 2008-07-29
  • 打赏
  • 举报
回复
up
blackscyther 2008-07-29
  • 打赏
  • 举报
回复
执行不报错,但是插入的数据为空。

用Response.Write(SqlCmd);
输出的插入语句为: insert into image(ImageData,ImageContentType,ImageDescription,ImageSize) values(@Image,@ContentType,@ImageDescription,@ImageSize)

变量的值都没有带进去。我都困惑了好几天了......
sxmonsy 2008-07-29
  • 打赏
  • 举报
回复
楼主写的有什么问题吗?
没看出有问题呀.
blackscyther 2008-07-29
  • 打赏
  • 举报
回复
那像程序里的语句,用MySql应该怎么写?
blackscyther 2008-07-28
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 Jinglecat 的回复:]
MySql 的语法跟 SQLServer 一样?
[/Quote]

MySql的语法跟SQLServer不一样么? 能说得详细点么
blackscyther 2008-07-28
  • 打赏
  • 举报
回复
我换了数据库的名称,错误依旧
blackscyther 2008-07-28
  • 打赏
  • 举报
回复
我在数据库里把所有的字段都设置为 允许为空 它才提示上传成功
Jinglecat 2008-07-28
  • 打赏
  • 举报
回复
MySql 的语法跟 SQLServer 一样?
myhope88 2008-07-28
  • 打赏
  • 举报
回复
有没有提示较图片上传成功了呢,建议表名不用Image,好像是关键字
blackscyther 2008-07-28
  • 打赏
  • 举报
回复
up
blackscyther 2008-07-28
  • 打赏
  • 举报
回复
up

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧