Simple DataBase connection

Posted on 2004-10-07
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.
Question by:calinutz
  • 5
  • 4
LVL 19

Expert Comment

ID: 12252363
LVL 19

Expert Comment

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:


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
LVL 11

Author Comment

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.
LVL 19

Expert Comment

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


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.
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

LVL 19

Accepted Solution

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:

    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");
    System.Data.Odbc.OdbcCommand cmd = cxn.CreateCommand();
    cmd.CommandText = "SELECT * FROM Names;";
    cmd.CommandType = System.Data.CommandType.Text;
        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???
    catch (System.Data.Odbc.OdbcException ex)
        foreach ( System.Data.Odbc.OdbcError er in ex.Errors)
LVL 11

Author Comment

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.
LVL 11

Author Comment

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
LVL 19

Expert Comment

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?
LVL 11

Author Comment

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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Build JSON from table records 17 39 bundle 8 34
Expando 4 33
Is setting this variable making a duplicate in memory 4 11
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now