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.
Unfortunately, activation email could not send to your email. Please try again.

How do i insert doc files into a sql database

Thread ID:

Created:

Updated:

Platform:

Replies:

1250 Oct 8,2002 04:11 AM Nov 19,2002 12:01 PM Windows Forms 1
loading
Tags: FAQ
pix
Asked On October 8, 2002 04:11 AM

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

John
Replied On 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); }

CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;