我用SQLserver建了一个表,表名是News,表中的一个属性是Image,想用来存储图片。
请问 insert语句该怎么写呢?Image的值是图片的路径吗?
另外在页面中显示的时候,该用什么控件显示呢?是Image控件吗?

解决方案 »

  1.   

    image值是要存文件本身
    不过现在通常只存文件路径
      

  2.   

    FileInfo file = new FileInfo(UploadFile.Value);
    Stream imgage = UploadFile.PostedFile.InputStream;
    //文件大小
    int l = UploadFile.PostedFile.ContentLength;
    string type = UploadFile.PostedFile.ContentType;
    byte[] imgdata = new byte[l];
    int n = imgage.Read(imgdata,0,l);
    //SqlConnection对象
    SqlConnection conn = new SqlConnection("Server=.;Uid=sa;Pwd=;DataBase=pubs");
    SqlCommand cmd = conn.CreateCommand();
    cmd.CommandText = "INSERT INTO ImageStore(IMGData,IMGType,IMGTitle) VALUES(@img,@type,@name)";
    //参数FileData
    cmd.Parameters.Add("@img",SqlDbType.Image);
    cmd.Parameters["@img"].Value = imgdata;            
    cmd.Parameters["@img"].Direction = ParameterDirection.Input;
    //参数FileType
    cmd.Parameters.Add("@type",SqlDbType.NVarChar,50,"ImageType");
    cmd.Parameters["@type"].Value = type;
    cmd.Parameters["@type"].Direction = ParameterDirection.Input;
    //参数FileName
    cmd.Parameters.Add("@name",SqlDbType.NVarChar,50);
    cmd.Parameters["@name"].Value = getFileName(UploadFile.Value);
    cmd.Parameters["@name"].Direction = ParameterDirection.Input;
    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();
    cmd.Dispose();
    conn.Dispose();
      

  3.   

    Sql Server 数据库:http://support.microsoft.com/default.aspx?scid=kb;EN-US;309158Oracle 数据库:http://www.chinamacro.com/blog/visit_detail.aspx?blogID=53
      

  4.   

    谢谢50277(柳影随风)和morality。
      

  5.   

    50277(柳影随风)写的已经可以了,
    我这里再贴一个ORACLE的写法,参考一下.//上传
    if(myFile.ContentLength != 0)
    {
      try
      {
        System.Web.HttpPostedFile myFile = this.Request.Files[0];
    //        string tmpFileName = myFile.FileName;
    //        string myFileName = tmpFileName.Substring(tmpFileName.LastIndexOf("."));
    //        string myFileMimeType = myFile.ContentType();
    //        myFile.SaveAs(this.Server.MapPath("../" + myFileName));    //读取到数组里面
        System.IO.Stream mystream = myFile.InputStream;
        byte[] Buffer = new byte[myFile.ContentLength];
        mystream.Read(Buffer,0,myFile.ContentLength);    //打开数据库
        OracleConnection cn = new OracleConnection(ConfigurationSettings.AppSettings["sysDSN"]);
        cn.Open();    //用参数方式写入数据库
        OracleCommand myComm = cn.CreateCommand();
        string sql = "insert into tmp(tmp_id,tmp_blob) values(tmp_seq.nextval,:tmp_blob)";
        myComm.CommandText = sql;
        myComm.Parameters.Add(":tmp_blob",OracleType.Blob,Buffer.Length).Value = Buffer;
        myComm.ExecuteNonQuery();
      }
      catch
      {
        //此处可加错误显示
      }
      finally
      {        
        cn.Close();
      }
    }