?
Solved

C# and MS Access connection and access in windows application

Posted on 2008-02-02
10
Medium Priority
?
1,377 Views
Last Modified: 2013-12-17
hello,

I need a simple code to connect my program to a db.  And another to retrieve data from a single cell.

So far the only thing I've done is include System.Data.OleDb  and inserted the following code after
InitializeComponent();

            OleDbConnection clsConnection;
            clsConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\Projects\app1\app1\\DB1.mdb;Persist Security Info=False");
            clsConnection.Open();

I don't know why I keep receiving a runtime error:

Format of the initialization string does not conform to specification starting at index 56.
0
Comment
Question by:Serph
10 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1000 total points
ID: 20804994
you are missing some \ to be duplicated.
also, the Persist Security info is invalid...
 
            OleDbConnection clsConnection;
            clsConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\Projects\\app1\\app1\\DB1.mdb;");
            clsConnection.Open();

Open in new window

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20804995
0
 
LVL 13

Expert Comment

by:kaylanreilor
ID: 20805017
What happen if you write "Data Source=E:\Projects\app1\app1\DB1.mdb"
Let's also have a look to this website: http://www.connectionstring.com/
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 1

Author Comment

by:Serph
ID: 20805706
Thank you angelIII, it works!

But can you tell me how to retrieve a single cell and store it in a variable? I just need to know how the statement should be.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20805757
a single cell? you mean, from 1 table, 1 specific row, 1 specific column? ...

you "simply" need to put the necessary SQL as CommandText into a OleDbCommand object, and use ExecuteScalar() method, which will return that value...
0
 
LVL 1

Author Comment

by:Serph
ID: 20805870
I'm a little confused, Is this correct?  I'm kind of clueless here!?
tt="SELECT * FROM gramm_rules WHERE Rule_ID='1'";
OleDbCommand cmd=new OleDbCommand(tt,clsConnection);
cmd.CommandText="SELECT * FROM gramm_rules WHERE Rule_ID='1'";
p = Convert.ToInt32(cmd.ExecuteScalar());

Open in new window

0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 1000 total points
ID: 20805880
don't use *, but the column name you want to retrieve.
0
 
LVL 1

Author Comment

by:Serph
ID: 20805934
My bad, I changed it but I keep getting a runtime error: Type Mismatch, even though I convert it from object to string.
Thank you for your help, my knowledge in C# is very limited.

st=Convert.ToString(cmd.ExecuteScalar());

Open in new window

0
 
LVL 2

Assisted Solution

by:Syed Irtaza Ali
Syed Irtaza Ali earned 200 total points
ID: 20839247
Normally the code is done like this...

 

DataSet dataSet = new DataSet();
daRules = new OleDbDataAdapter();
tt="SELECT * FROM gramm_rules WHERE Rule_ID='1'";
daRules.SelectCommand = new OleDbCommand(tt, conn);
try {
if (conn.State == ConnectionState.Closed)
    conn.Open();
daRules.Fill(dataSet, "RulesTable");
conn.Close();
 
DataView dvRules = dataSet.Tables["RulesTable"].DefaultView;
DataRow dr = dvRules[0].Row;
txtValue1 = dr["Column1"].ToString();
 
}
catch (Exception ex)
{
   MessageBox.Show(ex.ToString());
}

Open in new window

0
 
LVL 1

Author Closing Comment

by:Serph
ID: 31427485
Thank you for your help, I'm now taking the time to expand my knowledge in C#.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Native ability to set a user account password via AD GPO was removed because the passwords can be easily decrypted by any authenticated user in the domain. Microsoft recommends LAPS as a replacement and I have written an article that does something …
Planning to migrate your EDB file(s) to a new or an existing Outlook PST file? This video will guide you how to convert EDB file(s) to PST. Besides this, it also describes, how one can easily search any item(s) from multiple folders or mailboxes…
Free Data Recovery software is an advanced solution from Kernel Tools to recover data and files such as documents, emails, database, media and pictures, etc. It supports recovery from physical & logical drive after a hard disk crash, accidental/inte…
Suggested Courses

589 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question