• 0

Data(base) access in C# / .Net


Question

In VB6, there's a DataGrid control which I can use to easily hook up to a MS Access database, and I can read and display the contents of a table in the grid. I know database access in C#, or even .Net in general is different from back then.. I have even read the basics of data access in .Net, so I have rough ideas on things like DataSet and DataAdapters.

My question is: how can I do the same thing with C# specifically, and .Net in general? And what if my database is a Mysql database residing on localhost? I have MySQL 3.23.54 & IIS 5.1 installed.

Wherever possible, I want to work only with Access / Mysql databases, since I am not familiar with MS SQL server.

Right now, I have only managed to establish connection to an Access DB with ODBC drivers, but even with that, I cannot display / manipulate it from my program written in C#.

Help needed. Thanks in advance.

Link to comment
https://www.neowin.net/forum/topic/58461-database-access-in-c-net/
Share on other sites

6 answers to this question

Recommended Posts

  • 0

You need:

MDAC 2.7 (or higher)

ODBC.NET

MySQL

MyODBC (find it on the MySQL home page)

Then use the following code:

using Microsoft.Data.Odbc;

...

OdbcConnection connection = new OdbcConnection("Driver={MySQL};SERVER=localhost;UID=<username>;PWD=<passwor d>;DATABASE=<databasename>;");

connection.Open();

...

This uses the ODBC drivers to communicate with the database. There is apparently an effort to create a native data provider for MySQL (like MS has done with SQL server). But I'm pretty sure it's not ready yet.

Hope this helps.

  • 0

Yes you still need it because ODBC.NET was released after NET Framweork v1.0 went RTM.

You can d/l from <a href='http://msdn.microsoft.com/downloads/default.asp?url=/downloads/sample.asp?url=/msdn-f iles /027/001/668/msdncompositedoc.xml' target='_blank'>http://msdn.microsoft.com/downloads/defaul...ompositedoc.xml</a&g t;

Related to the Access question:

1. You connect to the DB:

string scon = @"Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=c:\My Documents\krp.mdb";

OleDbConnection con = new OleDbConnection(scon);

con.Open();

2. Create the Data adapter

OleDbDataAdapter odb = new OleDbDataAdapter("SELECT * FROM MyTable", con);

DataSet ds = new DatSet();

//fill dataset

odb.Fill(ds);

Note : If you select data from only one table you can use a data table instead

of a dataset. It's lighter.

3. You bind the data set to grid

this.DataGrid.DataSource = this.ds.Tables[0].DefaultView;

4. You can edit now .....watch for the data set's events ;) :yes:

  • 0

Gumboot: OK. I have successfully installed MyODBC, and made the necessary config in the ODBC Admin tool. But the code snippet u provided doesn't really get me anywhere. I am new to C#. Can u lead me to a sample code - that demos manipulation of mysql / access DBs in C#, like in some sort of data grid?

Many thanks.

  • 0

Did you manage to get ODBC.NET? Go to msdn.microsoft.com and search for ODBC.NET if you haven't (it should be the first hit). [ BTW, anyone else having problems posting links? They seem to have spaces inserted into them! ]

Don't have any experience with datagrids, sorry. But if you want to use MySQL, replace OleDbConnection with OdbcConnection and OleDbDataAdapter with OdbcDataAdapter. Also, make sure you add Microsoft.Data.Odbc.dll to your project references. And add "using Microsoft.Data.Odbc;" to the top of your source code.

Edited by Gumboot
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Intel's next-gen desktop processors leak with a whopping number of cores by Taras Buria Intel's third-generation Core Ultra processors for desktop computers are on the way. If the latest leak is true, we are up for some significant updates in the Core Ultra 300 Series. @g01d3nm4ng0 on X posted a list of Intel's upcoming desktop processors, which includes seven SKUs ranging from entry-level Core Ultra 3 models to the flagship Core Ultra 9 with a rather whopping number of cores. According to the leak, the top-end model will feature 52 cores without hyperthreading. However, those cores are not like "classic" cores found in HEDT chips like AMD's Threadrippers. Instead, those 52 cores are divided into three categories: performance, efficiency, and low-power. The upcoming Core Ultra 9 reportedly features 16 performance cores, 32 efficiency cores, and 4 low-power cores. The cheapest Core Ultra 3 has four of each, 12 cores in total. Low-power cores are new to Intel's desktop processors. The current Core Ultra 200 chips feature the same hybrid configuration that was introduced with the 12th-gen Alder Lake chips: performance cores for demanding tasks and efficiency cores for lightweight tasks (up to 24 cores). Now, Intel wants to improve the efficiency of its chips further by adding another set of low-power cores, which were first introduced in the first-gen Core Ultra mobile chips (Meteor Lake). @jaykihn0 adds that Intel Nova Lake-S will support 8000 MT/s memory speed by default and 32 PCIe Gen 5 lanes and 16 PCIe Gen 4 lanes, totalling 48 lanes in CPU + chipset configuration. SKU P Cores E Cores LP Cores TDP Core Ultra 9 16 32 4 150W Core Ultra 7 14 24 4 150W Core Ultra 5 8 16 4 125W Core Ultra 5 8 12 4 125W Core Ultra 5 6 8 4 125W Core Ultra 3 4 8 4 65W Core Ultra 3 4 4 4 65W Today's leak aligns with previous reports about the upcoming Intel Nova Lake-S processors. However, Intel enthusiasts should probably get ready to switch platforms again, as the Nova Lake-S generation is rumored to require a brand-new LGA1954 socket.
    • My mb got a bios update but it just says... is this the fix? - Fix "AMD fTPM switch" option will be hidden. as long as set AMD CPU fTPM to Disabled - AGESA PI-1.2.0.3c released.
  • Recent Achievements

    • Explorer
      treker_ed went up a rank
      Explorer
    • Apprentice
      CHUNWEI went up a rank
      Apprentice
    • Veteran
      1337ish went up a rank
      Veteran
    • Rookie
      john.al went up a rank
      Rookie
    • Week One Done
      patrickft456 earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      656
    2. 2
      ATLien_0
      272
    3. 3
      +FloatingFatMan
      176
    4. 4
      Michael Scrip
      157
    5. 5
      Steven P.
      136
  • Tell a friend

    Love Neowin? Tell a friend!