We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

how to upload files to SQl database from C#.NET windows from applications

hi,
I am using SQL client as the database and VS 2008 standard to bild my project. i need to browse for a file and upload it to the SQL database. how can I do this ??
I have the code to browse for the file and insert to the database, but it dosen't work, can someone hilp me with this. My code is shown below.
My code is onlt for pictures how can i upload different format of files. I need to save the path of the file and the file in the database , how can i do this.
My code

OpenFileDialog dlg = new OpenFileDialog();
            DialogResult dlgRes = dlg.ShowDialog();
            if (dlgRes != DialogResult.Cancel)
            {
                //Set image in picture box
                pictureBox1.ImageLocation = dlg.FileName;

                //Provide file path in txtImagePath text box.
                txtImagePath.Text = dlg.FileName;

                String query = @"Insert ImagesStore(OriginalPath,ImageDarta) Values ('" + txtImagePath.Text + ",'" + pictureBox1.ImageLocation + "')";
                SqlCommand command = new SqlCommand(query, DB.getConnection());
                db.openConnection();
                command.ExecuteNonQuery(); 
                db.closeConnection();

THERE IS AN ERROR IN command.ExecuteNonQuery(); , IT SAYS "Incorrect syntax near 'C:'.
Unclosed quotation mark after the character string ')'."


THE TABLE IS ImageStore :

OriginalPath varchar(MAX)
ImageData varchar(MAX)

WHAT IS THE ERROR IN THIS LINE

PLEASE CAN I GET A ANSWER FOR MY QUESTION
THANK YOU

5
Contributors
7
Replies
1 Year
Discussion Span
1 Year Ago
Last Updated
12
Views
krishnisilva
Junior Poster
122 posts since Apr 2010
Reputation Points: 10
Solved Threads: 4
Skill Endorsements: 0

Improper syntax.

String query = @"Insert  INTO ImagesStore (OriginalPath,ImageDarta) Values ('" + txtImagePath.Text + "','" + pictureBox1.ImageLocation + "')";
__avd
Posting Genius (adatapost)
Moderator
8,737 posts since Oct 2008
Reputation Points: 2,141
Solved Threads: 1,262
Skill Endorsements: 51

hey thanks,
how do i store doc, pdf files in the SQL database? is it the same way can you help me in this? i want the path and the file to be stored in the database? please an u help me in this,
thanx

krishnisilva
Junior Poster
122 posts since Apr 2010
Reputation Points: 10
Solved Threads: 4
Skill Endorsements: 0

>i want the path and the file to be stored in the database

Firstly, your table column type must be Image to store binary data (file content).

Secondly, you need to write parametrized query.

SqlConnection cn=new SqlConnection("put_conn_str");
String query = @"INSERT  INTO ImagesStore (OriginalPath,ImageDarta) Values (@path,@data)";
SqlCommand cmd=new SqlCommand(query,cn);

//read file
byte []barray=System.IO.File.ReadAllBytes(dlg.FileName);

//extract the filename only from the selected path
string filename=System.IO.Path.GetFileName(dlg.FileName);

// create sql parameters

cmd.Parameters.Add("@path",SqlDbType.VarChar,100).Value=filename;
cmd.Parameters.Add("@data",SqlDbType.Image,barray.Length).Value=barray;

cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
__avd
Posting Genius (adatapost)
Moderator
8,737 posts since Oct 2008
Reputation Points: 2,141
Solved Threads: 1,262
Skill Endorsements: 51
String query = @"Insert ImagesStore(OriginalPath,ImageData) Values ('" + txtImagePath.Text + "','" + pictureBox1.ImageLocation + "')";

err try that

ds2r
Light Poster
27 posts since Aug 2009
Reputation Points: 10
Solved Threads: 2
Skill Endorsements: 0

i want to save files using vs 2008 c# windows application into my database sql server 2005. after saving files i want to open that files, please send me the code for that. its urgent pls.

jerish
Newbie Poster
2 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

may be you should try this code:

private void button4_Click(object sender, EventArgs e)
    {
        try
        {
            int f1 = 0;
            string p1;
            p1 = openFileDialog1.FileName;
            FileStream fs = new FileStream(p1, FileMode.Open);
            byte[] buffer = new byte[fs.Length];
            fs.Read(buffer, 0, (int)fs.Length);
            fs.Close();
            string query = "insert into test (data) values (@data_file)";
            SqlCommand com = new SqlCommand(query, con);
            com.Parameters.AddWithValue("@data_file", buffer);
            con.Open();
            com.ExecuteNonQuery();
            {
                f1 = 1;
            }
            if (f1 == 1)
            {
                MessageBox.Show("File saved into database");

            }
        }
        catch (Exception e1)
        {
            MessageBox.Show(e1.Message);
        }

    }
jerish
Newbie Poster
2 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

string path;
openFileDialog opendlg = new openFileDialog();
if(opendlg.showdialog() == DialogResult.ok)
{

path = opendlg.fileName;
}
sqlconnection conn = new sqlconnection("put your connection");
sqlcommand cmd = conn.createCommand();

try
{
conn.open();
cmd.commandText = "INSERT INTO ImagesStore (OriginalPath)values ('"+path+"')";
cmd.executeNonquery();


conn.close();
}
catch(Exception e)
{
MessageBox.show(e.message);
}

LP...
Newbie Poster
6 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0958 seconds using 2.79MB