Check if value exists in Access Database using c#

I wish to enter data into my access database table like so:

private OleDbConnection mycon;
string sDBPath = Application.StartupPath;
string sConnection = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}\\MyAccessDB.mdb;Persist Security Info=False", sDBPath);

mycon = new OleDbConnection(sConnection);
OleDbCommand cmd = mycon.CreateCommand();
   cmd.CommandText = "insert into MyTable values('" + sID + "','" + sData1+ "','" + sData2+ "','" + sData3+ "','" + sData4+ "');";

cmd.ExecuteNonQuery();
 mycon.Close();

My question is how do I check if an entry already exists in the database row.  e.g. the first variable sID above?

Thanks
Wanting2LearnManAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ErezMorCommented:
youll need to run another command before the one youre using to check for that
something like
cmd.CommandText = "Select Count(field) From MyTable Where sID='" + sID + "');";

if cmd.ExecuteScalar()>0
 cmd.CommandText = "Update MyTable set sData1='" + sData1+ "',sData2='" + sData2+ "',sData3='" + sData3 + "'sData4=,'" + sData4 + "' Where sID=" + sID +";

else
cmd.CommandText = "insert into MyTable values('" + sID + "','" + sData1+ "','" + sData2+ "','" + sData3+ "','" + sData4+ "');";

cmd.ExecuteNonQuery();
 
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
anarki_jimbelSenior DeveloperCommented:
One way would be to search the value in DB and check if there are rows returned
0
ddayx10Commented:
Well since you are inserting a new row the row will not have any values so I think what you are asking is how do you check to see if a particular piece of data is already defined by a key value and update if it already exists otherwise insert a new row?

The answer is you either have to write more complex sql that will do this for you, or check first before you decide what to do. I have written a sample of what the latter might look like below.

dday
        string sDBPath = Application.StartupPath;
        string sConnection = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}\\MyAccessDB.mdb;Persist Security Info=False", sDBPath);
 
        mycon = new OleDbConnection(sConnection);
        OleDbCommand cmd = mycon.CreateCommand();
        cmd.CommandText = "SELECT * FROM MyTable WHERE [columnName] = " + sID;
 
        OleDbDataReader dr = cmd.ExecuteReader();
 
        if (dr.HasRows())
        {
            dr.Close();
            cmd.CommandText = "SQL UPDATE STATEMENT HERE";
        }
        else
        {
            dr.Close();
            cmd.CommandText = "insert into MyTable values('" + sID + "','" + sData1 + "','" + sData2 + "','" + sData3 + "','" + sData4 + "');";
        }
 
        cmd.ExecuteNonQuery();
        mycon.Close();

Open in new window

0
käµfm³d 👽Commented:
What about:
string sDBPath = Application.StartupPath;
string sConnection = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}\\MyAccessDB.mdb;Persist Security Info=False", sDBPath);
 
mycon = new OleDbConnection(sConnection);
OleDbCommand cmd = mycon.CreateCommand();
 
cmd.CommandText = string.Format("Select count(1) from MyTable where sIDColumnName = '{0}'", sID);
 
int count = Convert.ToInt32(cmd.ExecuteScalar());
 
if (count == 0)
{
    cmd.CommandText = "insert into MyTable values('" + sID + "','" + sData1 + "','" + sData2 + "','" + sData3 + "','" + sData4 + "');";
 
    cmd.ExecuteNonQuery();
}
 
mycon.Close();

Open in new window

0
ddayx10Commented:
Ok so as usual by the time I can write something there are already answers available that are perfectly acceptable :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.