用byte形式存入
读出后还原成图片
Image类

解决方案 »

  1.   

    switch(this.fileLoad.PostedFile.ContentType)
    {
        case "image/bmp":
        if(this.fileLoad.PostedFile.InputStream.Length > 1024000)
    //图片大小大于1000kb
        {
    this.H_Error.Value = "imageSizeError";
        }
        else
        {
    Stream imgDataStream = this.fileLoad.PostedFile.InputStream;
    int imgDataLenth = this.fileLoad.PostedFile.ContentLength;
    byte[] imgDataList = new byte[imgDataLenth];
    imgDataStream.Read(imgDataList,0,imgDataLenth);
    Session["sImage"] = imgDataList;
    Image.ImageUrl = "ShowFile.aspx";
    //以上实在web窗体中显示图片,下面是保存到数据库中
    string updateSql = "Select  from unformatdata where ID = " + Session["sID"].ToString();
    //创建sqlCommand对象
    string conString = "server = (local);database = pratice;user id = sa; pwd = ";
    SqlConnection myConnection = new SqlConnection(conString);
    SqlCommand myCommand = new SqlCommand(updateSql,myConnection);
    myCommand.Connection.Open();
    try
    {
    SqlDataAdapter MyAdapter=new SqlDataAdapter();
    SqlCommandBuilder Var_SqlCB;
    MyAdapter.SelectCommand=myCommand;
    Var_SqlCB = new SqlCommandBuilder(MyAdapter);
    DataSet myDataSet=new DataSet();
    MyAdapter.Fill(myDataSet,"myTable");
    myDataSet.Tables["myTable"].Rows[0]["Unfordata"] = (byte[])imgDataList;
    myDataSet.Tables["myTable"].Rows[0]["Type"] = "bmp图像";
    myDataSet.Tables["myTable"].Rows[0]["Size"] = imgDataLenth;
    MyAdapter.Update(myDataSet,"myTable");
    }
    catch(Exception ex)
    {
    System.Console.WriteLine(ex.Message.ToString());
    }
    myCommand.Connection.Close(); DataGrid1.DataSource = fillDT();
    DataGrid1.DataBind();
    }
    break;
    //类似的还可以添加声音、动画、word等
    //读出代码如下:
    switch(dataType)
    {
      case("bmp图像"):
      Session["FileType"] = "image/bmp";
      Session["sImage"] = chooseDataLine["Unfordata"];
      this.Image.ImageUrl = "ShowFile.aspx";
      break;
    //类似的可以显示其他数据