Error Inserting Image into Database [duplicated]

0

I have been trying several ways to insert an image in a database With Sql Server of different methods and none could work in which I send some errors, I can store data types string and int type without any problem but I can not save an image.

Error:

  

Error related to the network or specific to the instance while establishing a connection to the SQL Server. The server was not found or it was not accessible. Verify that the name of the instance is correct and that SQL Server is configured to support remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection with SQL Server)

This time I was guided using the same steps and código of this video   Sql Server Tutorial

  

Error Image

  

Full Code

SqlConnection conn = new SqlConnection("Data Source=vsc; Initial Catalog=HRIS; User ID=sa; Password=vsc");
    SqlCommand command;
    string imgLoc = "";
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void buttonBrowse_Click(object sender, EventArgs e)
    {
        try
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif|All Files (*.*)|*.*";
            dlg.Title = "Select Employee Picture";
            if(dlg.ShowDialog() == DialogResult.OK)
            {
                imgLoc = dlg.FileName.ToString();
                picEmp.ImageLocation = imgLoc;  
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }                 

    }

    private void buttonSave_Click(object sender, EventArgs e)
    {
        try
        {
            byte[] img = null;
            FileStream fs = new FileStream(imgLoc, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);
            img = br.ReadBytes((int)fs.Length);
            string sql = "INSERT INTO Employee(EID,FIRST_NAME,LAST_NAME,IMAGE)VALUES("+textBoxEID.Text+",'"+textBoxFName.Text+"','"+textBoxLName.Text+"',@img)";

            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
                command = new SqlCommand(sql, conn);
                command.Parameters.Add(new SqlParameter("@img", img));
                int x = command.ExecuteNonQuery();
                conn.Close();
                MessageBox.Show(x.ToString() + " record(s) saved.");
            }

            }

        catch(Exception ex)
        {
            conn.Close();
            MessageBox.Show(ex.Message);
        }
    }
}
    
asked by Diego 08.04.2017 в 00:33
source

1 answer

0

At a glance, your connectionString Try to do this in your constructor method to validate that it is ok:

public Form1()
{
    InitializeComponent();
    try
    {
       conn.Open();
       MessageBox.Show("Conexión establecida");
    }
    catch(Exception ex)
    {
       MessageBox.Show(ex.Message);
    }
}

If you get the same mistake then you were right

    
answered by 08.04.2017 / 00:41
source