大虾请看:
byte []bytes=null;//外部变量
浏览图片的viewclick()事件:
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
this.file = this.openFileDialog1.FileName;
FileStream fs = new FileStream(this.file, FileMode.Open);
bytes = new byte[fs.Length];
BinaryReader br = new BinaryReader(fs);
bytes = br.ReadBytes(Convert.ToInt32(fs.Length));
}
保存postclick()事件:
string sql = "insert into [user](id,pwd,sex,image)" +
" values('" + this.tid + "','" + this.tpwd + "','" + this.tsex + "',@image" ;
Execmd(sql, bytes);//问题1:怎么使得viewclick()里的bytes值传给postclick()里的bytes
Execmd方法定义如下:
public void Execmd(string sql,byte []bytes)
{
SqlConnection con =new Sqlcnnection();
SqlCommand cmd =new Sqlcommand();
SqlParameter para = new SqlParameter();
con.Open();
cmd.Connection = con;
cmd.CommandText = sql;
para.ParameterName = "image";
para.SqlDbType = SqlDbType.Image;
para.Value = bytes;
cmd.Parameters.Add(para);
cmd.ExecuteNonQuery(); //问题2:运行,提示“列名image无效”,数据库里存放图片的字段名为image
con.Close();
}
byte []bytes=null;//外部变量
浏览图片的viewclick()事件:
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
this.file = this.openFileDialog1.FileName;
FileStream fs = new FileStream(this.file, FileMode.Open);
bytes = new byte[fs.Length];
BinaryReader br = new BinaryReader(fs);
bytes = br.ReadBytes(Convert.ToInt32(fs.Length));
}
保存postclick()事件:
string sql = "insert into [user](id,pwd,sex,image)" +
" values('" + this.tid + "','" + this.tpwd + "','" + this.tsex + "',@image" ;
Execmd(sql, bytes);//问题1:怎么使得viewclick()里的bytes值传给postclick()里的bytes
Execmd方法定义如下:
public void Execmd(string sql,byte []bytes)
{
SqlConnection con =new Sqlcnnection();
SqlCommand cmd =new Sqlcommand();
SqlParameter para = new SqlParameter();
con.Open();
cmd.Connection = con;
cmd.CommandText = sql;
para.ParameterName = "image";
para.SqlDbType = SqlDbType.Image;
para.Value = bytes;
cmd.Parameters.Add(para);
cmd.ExecuteNonQuery(); //问题2:运行,提示“列名image无效”,数据库里存放图片的字段名为image
con.Close();
}
http://www.cnblogs.com/wfyfngu/archive/2008/10/09/1307390.html
http://www.cnblogs.com/zyc2/archive/2004/07/05/21524.aspx
StringBuilder strSql=new StringBuilder();
strSql.Append("insert into pub_info(");
strSql.Append("pub_id,logo,pr_info)");
strSql.Append(" values (");
strSql.Append("@pub_id,@logo,@pr_info)");
SqlParameter[] parameters = {
new SqlParameter("@pub_id", SqlDbType.Char,4),
new SqlParameter("@logo", SqlDbType.Image,16),
new SqlParameter("@pr_info", SqlDbType.Text)};
parameters[0].Value = model.pub_id;
parameters[1].Value = model.logo;
parameters[2].Value = model.pr_info;
StringBuilder strSql=new StringBuilder();
strSql.Append("insert into pub_info(");
strSql.Append("pub_id,logo,pr_info)");
strSql.Append(" values (");
strSql.Append("@pub_id,@logo,@pr_info)");
SqlParameter[] parameters = {
new SqlParameter("@pub_id", SqlDbType.Char,4),
new SqlParameter("@logo", SqlDbType.Image),
new SqlParameter("@pr_info", SqlDbType.Text)};
parameters[0].Value = model.pub_id;
parameters[1].Value = model.logo;
parameters[2].Value = model.pr_info;
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, img);
byte[] bytes = ms.ToArray();
ms.Close();
GC.Collect();
还是不行,依然提示:列名image无效如果数据表里只有一个image字段,存入图片没问题
但表里有多个字段时,存入就会出问题
问题到底出在哪里呢?
As Insert Into [user](ID,pwd,sex,Photo) values(@ID,@pwd,@sex,@Image)
GO
然后 cmd = new SqlCommand("InsertImage ", con);
cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@ID", SqlDbType.类型);
cmd.Parameters["@ID"].Value = this.tid;
cmd.Parameters.Add("@pwd", SqlDbType.类型);
cmd.Parameters["@pwd"].Value = this.tpwd;
cmd.Parameters.Add("@sex", SqlDbType.类型);
cmd.Parameters["@sex"].Value = this.tsex; cmd.Parameters.Add("@Image", SqlDbType.Image);
cmd.Parameters["@UserImage"].Value = picbyte;
cmd.ExecuteNonQuery();
protected void Button1_Click(object sender, EventArgs e)
{
HttpPostedFile upFile = up_file.PostedFile;
int iFileLength = upFile.ContentLength;
try
{
if (iFileLength == 0)
{
Response.Write("<script>alert('Please choose the document which to upload!');</script>");
}
else
{
Byte[] FileByteArray = new Byte[iFileLength];
Stream StreamObject = upFile.InputStream;
StreamObject.Read(FileByteArray, 0, iFileLength);
string sql = "insert into tb_binaryImage(imgData,type,description,imgSize)values(@Image,@ContentType,@ImageDescription,@ImgSize)";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.Add("@Image", SqlDbType.Binary, iFileLength).Value = FileByteArray;
cmd.Parameters.Add("@ContentType", SqlDbType.VarChar, 50).Value = upFile.ContentType;
cmd.Parameters.Add("@ImageDescription", SqlDbType.VarChar, 200).Value = txtDesc.Text;
cmd.Parameters.Add("@ImgSize", SqlDbType.BigInt, 8).Value = upFile.ContentLength;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
//txtDesc.Text = "";
Response.Write("<script>alert('Upload Success!');</script>");
}
}
catch (Exception ex)
{
Response.Write("<script>alert('"+ex.Message.ToString()+"')</script>");
}
}
把 SqlCommand cmd =new Sqlcommand();
cmd.Connection = con;
cmd.CommandText = sql;
改为:Sqlcommand cmd=new Sqlcommand(con,sql);
就可以了
谢谢大家啦!!