Click here to Skip to main content
14,836,552 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i try build project database by access with C#
about add data for employees (ID-name-photo) and retrieve data

but the problem with me for save photo in database and retrieve

in main form i make start connection with database and i make close database when close project

not have problem with add record but my problem when i try add photo and when i retrieve it

this code for open connection

public partial class MainForm : Form
{
    public OleDbConnection connection = new OleDbConnection();
    public OleDbCommand cmd = new OleDbCommand();
    public MainForm()
    {
        InitializeComponent();
        connection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source = DataBaseEST.accdb";
    }
}


private void MainForm_Load(object sender, EventArgs e)
{
    OpenConnection();
}


private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
 {
     CloseConnection();
 }


What I have tried:

public partial class Employees_info : Form
{
    MainForm mainform = new MainForm();
    public OleDbCommand cmd = new OleDbCommand();

    private void Employees_info_Load(object sender, EventArgs e)
    {
        mainform.OpenConnection();
    }

    private void Employees_info_FormClosing(object sender, FormClosingEventArgs e)
    {
        mainform.CloseConnection();
    }

            private void btnAdd_Click(object sender, EventArgs e)
    {
        try
        {
        mainform.cmd.Connection = mainform.connection;
    mainform.cmd.CommandText = "insert into Employees_info (ID,EMP_Name) values ('" + txtID.Text + "','" + txtName.Text + "');
    mainform.cmd.ExecuteNonQuery();
                    MessageBox.Show("Data Saved");
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error " + ex);
        }
Posted
Updated 26-Dec-20 13:06pm

Don't do it like that. Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

And if you need help to insert the actual picture, try this: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^]
   
DO NOT open a connection to a database for the lifetime of an app, a form, or any other object.

ALWAYS open a connection to the database when you are going to go an operation on the database and close when you're not doing anything with the database. Open the connection as late as possible, do your work as quickly as possible, and close your connection as early as possible.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900