Following is the code snippet for saving a excelsheet file which is being browsed using Fileupload Control of ASP.Net in a Database. Here, i made a Table 'TestTable' in Northwind Database.
Contentlength method of Fileupload is used to get the length of excel file. This length is further used to assign length of a byte[] which will store the Filecontent.
Excelsheet file will be stored as Image in database.
if (FileUpload1.HasFile) { int intDocLength = FileUpload1.PostedFile.ContentLength; byte[] bytDocTemp = new byte[intDocLength]; byte[] IMAGE = FileUpload1.FileBytes;
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Northwind;Integrated Security=True"); con.Open(); SqlCommand cmd = new SqlCommand("insert into TestTable(FileExten,FileContent) values (@FileExt,@FileContent)"); cmd.Parameters.Add("@FileExt",SqlDbType.NVarChar); cmd.Parameters.Add("@FileContent",SqlDbType.Image); cmd.Parameters["@FileExt"].Value = FileUpload1.PostedFile.ContentType; cmd.Parameters["@FileContent"].Value = IMAGE; cmd.Connection = con; int i=cmd.ExecuteNonQuery();
|
No responses found. Be the first to respond and make money from revenue sharing program.
|