Uploading and retrieving images from SQL Server
Link: http://www.dotnetbips.com/displayarticle.aspx?id=60

解决方案 »

  1.   

    这里有上传和下载的代码:
    // private void Button1_Click(object sender, System.EventArgs e)
    // {
    // SqlConnection con = new SqlConnection("Server=yxg;uid=sa;pwd=;database=northwind");
    // SqlDataAdapter da = new SqlDataAdapter("Select * From MyImages", con);
    // SqlCommandBuilder MyCB = new SqlCommandBuilder(da);
    // DataSet ds = new DataSet("MyImages");
    //
    // da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
    // FileStream fs = new FileStream(@"E:\winnt\Gone Fishing.jpg", FileMode.OpenOrCreate, FileAccess.Read);
    //
    // byte[] MyData= new byte[fs.Length];
    // fs.Read(MyData, 0, System.Convert.ToInt32(fs.Length));
    //
    // fs.Close();
    //
    // da.Fill(ds,"MyImages");
    //
    // DataRow myRow;
    // myRow=ds.Tables["MyImages"].NewRow();
    //
    // myRow["Description"] = "This would be description text";
    // myRow["imgField"] = MyData;
    // ds.Tables["MyImages"].Rows.Add(myRow);
    // da.Update(ds, "MyImages");
    //
    // con.Close();
    // }
    //
    // private void Button2_Click(object sender, System.EventArgs e)
    // {
    // SqlConnection con = new SqlConnection("Server=yxg;uid=sa;pwd=;database=northwind");
    // SqlDataAdapter da = new SqlDataAdapter("Select * From MyImages",con);
    // SqlCommandBuilder MyCB = new SqlCommandBuilder(da);
    // DataSet ds = new DataSet("MyImages");
    //
    // byte[] MyData= new byte[0];
    //
    // da.Fill(ds,"MyImages");
    // DataRow myRow;
    // myRow=ds.Tables["MyImages"].Rows[0];
    //           
    // MyData=(byte[])myRow["imgField"];
    // int ArraySize = new int();
    // ArraySize = MyData.GetUpperBound(0); 
    //
    // FileStream fs = new FileStream(@"E:\winnt\Gone Fishing2.jpg", FileMode.OpenOrCreate, FileAccess.Write);
    // fs.Write(MyData, 0,ArraySize);
    // fs.Close();
    // }
      

  2.   

    private void Button1_Click(object sender, System.EventArgs e)
    {
    if (File1.PostedFile != null) 
    {
    try
    {
    //File1.PostedFile.SaveAs("c:\\"+TextBox1.Text);
    //fs.Read(imgData, 0,  fs.Length);  

    Stream imgdatastream = File1.PostedFile.InputStream;
    int imgdatalen = File1.PostedFile.ContentLength;
    string imgtype = File1.PostedFile.ContentType;//保存文件类型
    string imgtitle = TextBox1.Text;
    byte[] imgdata = new byte[imgdatalen];
    int n = imgdatastream.Read(imgdata,0,imgdatalen);
    // string connstr=((NameValueCollection)Context.GetConfig
    // ("appSettings"))["connstr"];
    SqlConnection connection = new SqlConnection("server=ljj;uid=sa;pwd=;database=testcdd");
    SqlCommand command = new SqlCommand("INSERT INTO ImageStore(imgtitle,imgtype,imgdata) VALUES ( @imgtitle, @imgtype,@imgdata )", connection );
                         SqlParameter paramTitle = new SqlParameter("@imgtitle", SqlDbType.VarChar,50 );
     paramTitle.Value = imgtitle;
    command.Parameters.Add( paramTitle); SqlParameter paramData = new SqlParameter( "@imgdata", SqlDbType.Image );
    paramData.Value = imgdata;
    command.Parameters.Add( paramData );
    SqlParameter paramType = new SqlParameter( "@imgtype", SqlDbType.VarChar,50 );
    paramType.Value = imgtype;
    command.Parameters.Add( paramType );
    connection.Open();
    int numRowsAffected = command.ExecuteNonQuery();
    connection.Close();

    }
    catch (Exception exc) 
    {
    TextBox1.Text=exc.Message+exc.InnerException+exc.HelpLink ;
    }
    } }private void Button2_Click(object sender, System.EventArgs e)
    {
    string s=Request.QueryString["TextBox1"];
    string imgid =Request.QueryString["imgid"];
    // string connstr=((NameValueCollection)
    // Context.GetConfig("appSettings"))["connstr"];
    SqlConnection connection = new SqlConnection("server=ljj;uid=sa;pwd=;database=testcdd");

    string sql="SELECT imgdata, imgtype FROM ImageStore ";//WHERE id = "+ imgid;
    // SqlConnection connection = new SqlConnection(connstr);
    SqlCommand command = new SqlCommand(sql, connection);
    connection.Open();
    SqlDataReader dr = command.ExecuteReader();
    if(dr.Read())
    {
    Response.ContentType = dr["imgtype"].ToString();
    Response.BinaryWrite( (byte[]) dr["imgdata"] );
    Response.End();
    }
    connection.Close(); }