Solved

Simple DataBase connection

Posted on 2004-10-07
10
260 Views
Last Modified: 2008-03-03
I need working code and explications about how to connect to a simple database such as MS SQL server DB or MS ACCESS. As I understood C# allows to connect directly to 4 database types. How about the other databases? For example if I want to connect to a Informix database what would I have to do to achieve this?
I hope someone can explain me from a delphi programmer point of view.
0
Comment
Question by:calinutz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 19

Expert Comment

by:drichards
ID: 12252363
.
0
 
LVL 19

Expert Comment

by:drichards
ID: 12252365
C# (.NET in general) lets you connect to any database with an OleDB or ODBC driver as well as Oracle and Sql Server via the System.Data.OleDb, System.Data.Odbc, System.Data.OracleClient, and System.Data.SqlClient namespaces, respectively.  If you have an ODBC or OleDb driver for informix, then you can use it.

You start with a Connection, and then your usage depends on what you are trying to achieve.  Each of the namespaces has a connection object:

    System.Data.Odbc.OdbcConnection
    System.Data.OleDb.OleDbConnection;
    System.Data.OracleClient.OracleConnection;
    System.Data.SqlClient.SqlConnection;

You need an appropriate connection string to initialize the connection and then you Open it.  Then you can create commands or use the data adapters to populate DataSets
0
 
LVL 11

Author Comment

by:calinutz
ID: 12254247
"You need an appropriate connection string to initialize the connection and then you Open it."

Please let me know exactly what steps I need to do to connect to my informix database:
hostname = srv_ix
server = servername
database = adaos
(ODBC) DSN = adaos
username = informix
password = pass

Next: what components should I place on my "form" and what properties do I need to set? Especially I need to know how to build that connection string.
Suppose I only need to query a table from the database... say : "select * from table1"

A piece of code would be also nice.
Thanks
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Expert Comment

by:drichards
ID: 12254874
If you've got a DSN defined, your connection string will be:

    "DSN=adaos"

and you should use the OdbcConnection.

I'll send you a couple of code options for reading the database when I get back in the office tomorrow.
0
 
LVL 19

Accepted Solution

by:
drichards earned 500 total points
ID: 12257497
It depends on how you want to display the data.  If you want to display one of the columns in a listbox, for example, the easiest thing to do is to use the DataAdapter and a DataSet.  In Visual Studio, go to the server explorer and add a new data connection.  Use the Microsoft OLE DB Provider for ODBC and then select the DSN name.  Test the connection and make sure it works (there's a "Test Connection" button on the wizard page where you enter the DSN).  Once that is working, open the for in the editor and drag the new connection onto your form.  It will create the connection object for you.  Then go to the Toolbox and drag an OdbcDataAdapter onto the form.  Fill in the appropriate information in the wizard.  Finally, right click on the DataAdapter it just created and select "Generate DataSet..." and fill in the information.  Now select the listbox and in the properties, set the 'DataSource' and 'DisplayMember' properties.

Finally, to make it all work, go into the form constructor (code view now) and add:

    odbcConnection1.Open();
    odbcDataAdapter1.Fill(dataSet1);  // Use correct names, of course

When you fire up the form, the listbox will have the appropriate data.


If you want more direct control over what's displayed, here's a way to read the data directly:

    System.Data.Odbc.OdbcConnection cxn = new System.Data.Odbc.OdbcConnection("DSN=adaos");
    cxn.Open();
    System.Data.Odbc.OdbcCommand cmd = cxn.CreateCommand();
    cmd.CommandText = "SELECT * FROM Names;";
    cmd.CommandType = System.Data.CommandType.Text;
    try
    {
        System.Data.Odbc.OdbcDataReader rdr = cmd.ExecuteReader();
        while (rdr.Read())
        {
            // Access the column data and populate form:  rdr.GetValue(<column_number>).ToString() or rdr.Get???
        }
        rdr.Close();
    }
    catch (System.Data.Odbc.OdbcException ex)
    {
        System.Diagnostics.Debug.WriteLine(ex.Message);
        foreach ( System.Data.Odbc.OdbcError er in ex.Errors)
        {
            System.Diagnostics.Debug.WriteLine(er.Message);
        }
    }
    cxn.Close();
0
 
LVL 11

Author Comment

by:calinutz
ID: 12296745
You will receive the points as soon as I succeed to build a small test app. And your answer looks very nice . I hope I will handle it well.
Sorry for the delayed reply but I was out of town for a couple of days.
I don't give you the points yet ... hoping to solve my problem first.
Maybe later today.
Thanks again.
0
 
LVL 11

Author Comment

by:calinutz
ID: 12487950
First of all I appologize for the delay. Then I will increase the points to 500 to show you that I appreciate your help. Then I must say that I did not succeed yet to build an app based on your input. My main problem may be that I use Borland C# Builder and not Microsoft's C#.
   I will try again these days, but if there is anything else you could add to help me I would appreciate it. Anyways seems like there's noone else interested in helping a begginer not even for points. If the question would have been placed in the Delphi area with a delphi subject there would be about 5 to 10 guys anxious to respond.
Thanks again.
I will close this question as soon as I will succeed building this silly application. In Delphi this is actual child play. I don't understand why are there no programers willing to share their knowledge. Because I am quite sure that most of you use databases in your applications...

 There are a lot of places for C# programmers on the web but I actuallly am involved in an important project right now and don't have the time to investigate myself the problem.
 So please give me a direction... something to start with in my C# Builder
0
 
LVL 19

Expert Comment

by:drichards
ID: 12500145
What problem(s) do you have?  The code I posted is working C# code.  It shouldn't make a difference what environment you use to compile it.  You just need to put in a valid DSN name and command text.

Do you get an exception?  If so, what's the error message?  If not, what is happening?
0
 
LVL 11

Author Comment

by:calinutz
ID: 12706075
In order to thank you for your assistance I will accept your answer an probabely re-ask this question again sometime later (in the next few weeks). I still didn't get to test your code because I am involved in a very important and time eating project in Delphi, so I haven't hat the chance to start C# ever since I replyied you last.
Thanks again for answering me in the first place. As I mentioned above, the experts in this area (C#) are not very comunicative
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

732 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