We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How do i insert doc files into a sql database

How do i insert doc files into a sql database using .net

1 Reply

JO John November 19, 2002 12:01 PM

public void SaveDoc() { openFileDialog1.Filter = "All (*.*)|*.*"; openFileDialog1.InitialDirectory = "c:\\" ; openFileDialog1.RestoreDirectory = true ; if (openFileDialog1.ShowDialog () == DialogResult.OK) { string fileName = openFileDialog1.FileName; if (fileName.Length != 0) { try { sFilePath = openFileDialog1.FileName; char a = '\\'; string[] ss = sFilePath.Split(a); string sFileName = ss[ss.GetUpperBound(0)]; FileStream fs = new FileStream (sFilePath,FileMode.Open,FileAccess.Read); Byte[] b = new Byte[fs.Length]; fs.Read(b,0,(int)fs.Length); fs.Close (); SqlConnection conn = new SqlConnection(sConn); SqlCommand command = new SqlCommand("INSERT INTO tblTest (Name,Data) VALUES (@Name,@Data)", conn); SqlParameter paramTitle = new SqlParameter("@Name", SqlDbType.VarChar,50 ); paramTitle.Value =sFileName; command.Parameters.Add( paramTitle); SqlParameter paramData = new SqlParameter( "@Data", SqlDbType.Image ); paramData.Value = b; command.Parameters.Add( paramData ); conn.Open(); int numRowsAffected = command.ExecuteNonQuery(); conn.Close(); } catch(Exception Ex) { MessageBox.Show(Ex.Message); } } } } private void LoadDoc() { char a = '\\'; string[] ss = sFilePath.Split(a); string sFileName = ss[ss.GetUpperBound(0)]; string sql="SELECT Name, Data FROM tbl WHERE Name = '"+sFileName+"'"; string sTempFile = ""; SqlConnection conn = new SqlConnection(sConn); SqlCommand command = new SqlCommand(sql, conn); conn.Open(); SqlDataReader dr = command.ExecuteReader(); if(dr.Read()) { sTempFile = Path.GetTempPath() + dr["Name"].ToString(); FileStream fs = new FileStream (sTempFile,FileMode.Create,FileAccess.Write); Byte[] b = (Byte[])dr["Data"]; fs.Write(b,0,(int)b.Length); fs.Close(); } conn.Close(); System.Diagnostics.Process.Start(sTempFile); }

Loader.
Live Chat Icon For mobile
Up arrow icon