Results 1 to 2 of 2

Thread: [RESOLVED] Saving and retrieving files to SQL

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    683

    Resolved [RESOLVED] Saving and retrieving files to SQL

    I am creating an electronic library and would like to store the actual files (not their location) on the sql server in a database. I was thinking about storing the files using BLOB since it will hold alot of data. I started with the code below but as I am new to this, I am unsure how I would save two files in the same save. I have the document file and another file as a cover page.

    Code:
    {
    SqlConnection con = new SqlConnection("Server=Darkover;uid=<username>;pwd=<strong password>;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(@"C:\winnt\Gone Fishing.BMP", 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();
    		
    }

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    683

    Re: Saving and retrieving files to SQL

    Nobody has saved two files in one database before?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width