Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
So I'm slamming my head against getting actually data from an Access database file.

I have set up a data connection, and I have tried tutorials, and it keep not working, so could somebody of you fine geniuses in here just show me a simple way of fetching data from the table "stars" in the file "10_stars.accdb" in C#. I'm using VS2010 if it matters.

(My background is in PHP and I am kinda stumbling into C# development)

thanks!

-frank

A bit of code:
C#
public void SetupStars()
{
    // create a new OdbcConnectiuon entity called "DbConnection"
    OdbcConnection DbConnection = new OdbcConnection("10_stars.accdb");

    // open DbConnection
    DbConnection.Open();

    // crates a varibale name for the SQL query text and adds Query
    OdbcCommand DbCommand = DbConnection.CreateCommand();
                DbCommand.CommandText = "SELECT * FROM ";

    OdbcDataReader DbReader = DbCommand.ExecuteReader();

    //count the number 
    int fCount = DbReader.FieldCount;

    // declare while loop counter variable
    int i = 0;

    while(i < fCount)
    {
        var dataReader = DbCommand.ExecuteReader();
        var indexOfColumnID = dataReader.GetOrdinal("X");
        var value1 = DbReader.GetValue(indexOfColumnID);

        // create the XAML-code as a string
        string newEllipse = "<Ellipse xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' Stroke='Black' Fill='Red' Height='30' Width='30' Margin='10,-10,10,-10'></Ellipse>";

        // Make string valid XML
        XmlReader reader = XmlReader.Create(new StringReader(newEllipse));

        // Add the XAML to the design
        starfield.Children.Add((Ellipse)XamlReader.Load(reader));

        // incriment while loop counter
        i++;
    }

    // Close DbConnection,
    DbReader.Close();
    DbCommand.Dispose();
    DbConnection.Close();
}

it's a bit messy, and badly commented!
Posted
Updated 12-Apr-12 7:03am
v2
Comments
ZurdoDev 12-Apr-12 12:02pm    
Can you post what you have done so far?
Frank R. Haugen 12-Apr-12 13:04pm    
see code snippet!
wizardzz 12-Apr-12 12:08pm    
Please don't hurt yourself Frank.
Frank R. Haugen 12-Apr-12 13:05pm    
ouch! too late :/
[no name] 12-Apr-12 12:18pm    
"...it keep not working..." Doesn't really give us much information to go on.

1 solution

C#
using System.Data.OleDb;


C#
string connection = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=yourpath\10_stars.accdb";
using (OleDbConnection con = new OleDbConnection(connection))
{
    con.Open();
    OleDbCommand cmd = new OleDbCommand("SELECT * FROM stars", con);
    OleDbDataReader reader = cmd.ExecuteReader();
}


Edit after your code posted.
Your connection string is incomplete. Your SQL statement does not include the name of the table to extract the data from.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900