当前位置:编程学习 > C#/ASP.NET >>

插入图片到数据库

在ASP里如何将Image控件里的图片插入到数据库里啊??
这是我的代码  可就是插不进,各位帮帮忙啊。。。。。。。。。


  protected void Button3_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(strCon);
       
            con.Open();
            SqlTransaction st = con.BeginTransaction();
            SqlCommand cmd = con.CreateCommand();
            cmd.Transaction = st;
            try
            {
                cmd.CommandText = "insert into Users(UserName,UserPwd,Ename,Email,Logo)values('" +this.txtname.Text+ "','" +this.txtpwd.Text+ "','" +this.txtename+ "','" +txtemail+ "','" +this.Image1+ "')";

                cmd.ExecuteNonQuery();
                 st.Commit();
                Response.Write("<Script>alert('注册成功!');location='javascript:history.go(-1)'</Script>");






            }
            catch(Exception  error)
            {
                Response.Write("<Script>alert('注册失败!');location='javascript:history.go(-1)'</Script>");
                st.Rollback();
            }
            con.Close();
        }
总是显示“注册失败”的错误。这是什么原因????(注:数据库里字段的类型是二进制的) --------------------编程问答-------------------- 晕死 。。。插图片做什么        插图片路径就好了啊    --------------------编程问答--------------------
引用 1 楼 liukaizxc 的回复:
晕死 。。。插图片做什么        插图片路径就好了啊

+1 到时候读取读取数据库路径就行了 --------------------编程问答-------------------- 图片这么插入。。。肯定不行 --------------------编程问答-------------------- 上传图片到一个文件夹中,数据库中存放图片的路径就行了…… --------------------编程问答-------------------- 最好 保存路径啊, 要保存图片,先把图片 转成byte[]流 

SqlCommand cmd=new SqlCommand("insert into fuser values (@i)",conn);
byte[] ib=new Byte[60000];
FileStream fs=new FileStream(@"*.jpg",FileMode.Open ,FileAccess.Read );
fs.Read(ib,0,60000);
cmd.Parameters.Add("@i",SqlDbType.Image,(int)fs.Length);
cmd.Parameters["@i"].Value=ib;
cmd.ExecuteNonQuery();
conn.Close();

--------------------编程问答-------------------- 存路径,要是非得存图片也是文件流的形式。。楼上说的对 --------------------编程问答-------------------- this.Image1??这是要获得什么
不要try-catch了,看报什么错 --------------------编程问答-------------------- 二进制图片显示

上传二进制
if (FileUpLogo.HasFile)
{
  //取得上传文件的大小
  int FileLen = FileUpLogo.PostedFile.ContentLength;
  Byte[] FileData = new Byte[FileLen];
  //创建访问客户端上传文件的对象
  HttpPostedFile hp = FileUpLogo.PostedFile;
  //创建数据流对象
  System.IO.Stream sr = hp.InputStream;
  //将图片数据放到FileData数组对象实例中,0代表数组指针的起始位置,FileLen代表指针的结束位置
  sr.Read(FileData, 0, FileLen);
  //将FileData 赋值给实体
  brandModel.fld_logo = FileData;
}

或者

HttpPostedFile upFile = up_file.PostedFile;//HttpPostedFile对象,用来读取上传图片的属性
            fileLength = upFile.ContentLength;//记录文件的长度
   try
   {
    if(fileLength==0)//当文件长度为0的时候
    {
     txtMessage.Text = "请选择要上传的文件!";
    }
    else
    {
     byte[] fileByte = new byte[fileLength];//用图片的长度来初始化一个字节数组存储临时的图片文件
     Stream fileStream = upFile.InputStream;//建立文件流对象
     fileStream.Read(fileByte,0,fileLength);//读取图片数据到临时存储体fileByte,0为数据指针位置,fileLength为数据长度
     string connString = "Data Source=192.168.1.250;database=image;uid=pwqzc;pwd=cn0088";
     SqlConnection conn = new SqlConnection(connString);//初始化数据库连接
     string insertStr = "insert into image (image_data,image_content_type,image_description,image_size) values (@image_data,@image_content_type,@image_description,@image_size)";
     //插入数据库语句
     SqlCommand comm = new SqlCommand(insertStr,conn);
     comm.Parameters.Add(new SqlParameter("@image_data",SqlDbType.Image));//添加参数
     comm.Parameters["@image_data"].Value = fileByte;//给参数赋值
     comm.Parameters.Add(new SqlParameter("@image_content_type",SqlDbType.VarChar,50));
     comm.Parameters["@image_content_type"].Value = upFile.ContentType;//记录图片类型
     comm.Parameters.Add(new SqlParameter("@image_description",SqlDbType.VarChar,50));
     comm.Parameters["@image_description"].Value = txtDescription.Text;//把其他的表单数据上传
     comm.Parameters.Add(new SqlParameter("@image_size",SqlDbType.Int,4));
     comm.Parameters["@image_size"].Value = upFile.ContentLength;//记录图片长度,读取数据的时候使用
     conn.Open();//打开数据库连接
     comm.ExecuteNonQuery();//添加数据
     conn.Close();//关闭数据库
     txtMessage.Text = "你已经成功的上传了图片";
    }
   }
   catch(Exception ex)
   {
       txtMessage.Text = ex.Message.ToString();
   }
  }
 }



读取


using(SqlConnection conn=new SqlConnection())  
{  
conn.ConnectionString="";  
   
string strSql="select * from Tb where Id='"+Id+"'";  
SqlCommand cmd=new SqlCommand(strSql,conn) ;  
conn.Open();  
SqlDataReader reader=cmd.ExecuteReader();  
   
if(reader.Read())  
{  
Response.ContentType = "application/octet-stream";  
Response.BinaryWrite((Byte[])reader["Photo"]);  
}  
Response.End();  
conn.Close();  
--------------------编程问答--------------------
引用 5 楼 yadoufeng 的回复:
最好 保存路径啊, 要保存图片,先把图片 转成byte[]流 

SqlCommand cmd=new SqlCommand("insert into fuser values (@i)",conn);
byte[] ib=new Byte[60000];
FileStream fs=new FileStream(@"*.jpg",FileMode.Open ,FileAccess.Rea……

+1 --------------------编程问答-------------------- 貌似这里写错了
cmd.CommandText = "insert into Users(UserName,UserPwd,Ename,Email,Logo)values('" +this.txtname.Text+ "','" +this.txtpwd.Text+ "','" +this.txtename+ "','" +txtemail.Text+ "','" +this.Image1+ "')";


//获取图片
this.Image1.ImageUrl.Substring(this.Image1.ImageUrl.LastIndexOf('/') + 1)
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,