CRUD Operations in win form application with access database

Hi to all,

In first post we connect with access database now in this post we make operations in database.

First we create one method that help to execute sql query in database.

public bool Executquary(string sQuary)
        {
            try
            {
//check connection
                if (dbConnection.State != ConnectionState.Open)
                {
                    dbConnection.Open();
                }
//create command
                OleDbCommand cmd = new OleDbCommand(sQuary);
                cmd.Connection = dbConnection;
                try
                {
//execute query
                    cmd.ExecuteNonQuery();
                    dbConnection.Close();
                    return true;
                }
                catch (OleDbException exo)
                {
                    dbConnection.Close();
                    throw exo;
                   
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
This function can execute our query in database.

Now we create one Table to perform operation.

Table name =Employee
columns Id=Autonumber,Name=Text,EmpNo=Text,Salary=Number

Here we use Autonumber datatype that automatically increment number for Id.

for insert new data :
string sQry="Insert into Employee (Name,EmpNo,Salary) Values ('Emp1','123456',10000)";

now call function Executquary(sQry);

same as for Update and Delete query is  :

Update:
string sQry="Update Employee Set Name='Emp01',EmpNo='123456',Salary=10100 Where Id=1";

Delete :
string sQry="Delete from Employee Where Id=1";

Thanks for read this post.

Comments

Popular posts from this blog

Populate combo box for month and year in c#

How to get month days count from date or month and year