1.11M Members

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

 
0
 

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

 
1
 

Improper syntax.

String query = @"Insert  INTO ImagesStore (OriginalPath,ImageDarta) Values ('" + txtImagePath.Text + "','" + pictureBox1.ImageLocation + "')";
 
0
 

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

 
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();
 
0
 
String query = @"Insert ImagesStore(OriginalPath,ImageData) Values ('" + txtImagePath.Text + "','" + pictureBox1.ImageLocation + "')";

err try that

 
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.

 
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);
        }

    }
 
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);
}

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: