Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# VS2010 Desktop OleDb
So this is a noobish question but I'm failing to get the correct results.
 
I have a table (Data Table 1) in a DB that I have successfully connected to and extracted to a DataSet. Now I used the MSDN-website provided example code to test the information, and the code (Code Snippet 1) prints out every piece of data flawlessly by going trough every row and and every column on the row and adding them to a string, (used only for testing purposes).
 
What I want is for a set of variables to be "edited" which is used in a function (Code Snippet 2) to ultimately add a new XAML element, which at some point will be a dynamic starmap.
 
I basically want to have a loop which populates a canvas with ellipses based on the information in the database's table's rows.
 
The code below is messy as hell, and just plain stupid in some places but It works, giving me a message box with the DB-table's contents in one long string. The data-table below is just for reference and are only a few columns of a whole lot.
 
thanks for any and all help!
 
Code snippet 1:
private void TestDBconnection()
{
 
    //Connecting to database
    string connetionString = null;
    OleDbConnection DbConn;
    connetionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Frank R. Haugen\Documents\Visual Studio 2010\Projects\StarApp\WpfApplication1\10_closest_stars.accdb;"; //TODO: make path relative
    DbConn = new OleDbConnection(connetionString);
 
    //SQL query string
    string SqlQueryString = "SELECT * FROM [10 closest stars]";
 
    //open connection to database
    DbConn.Open();
 
    //creat a dataset to hold all values
    DataSet myDataSet = new DataSet();
 
    //Setting up and starting connection to database-file
    OleDbConnection objConnection = new OleDbConnection(connetionString);
    OleDbCommand objCommand = new OleDbCommand(SqlQueryString, objConnection);
    objConnection.Open();
 
    //adapts the data from the database-file to a DataSet
    OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(objCommand);
    myDataAdapter.Fill(myDataSet, "[10 closest stars]");
 
    string Response = "";
 
    //testing
    foreach (DataTable table in myDataSet.Tables)
    {
        foreach (DataRow row in table.Rows)
        {
            foreach (DataColumn column in table.Columns)
            {
                Response += row[column] + ";";
            }
 
        //TODO: add function to add a new ellipse/star here

        }
    }
 
    MessageBox.Show(Response);
 
    objConnection.Close();
 
    //close connection to database
    DbConn.Close();
}
 
Data Table 1:
ID	HYG	GLIESE	SERV	
32263	32349	Gl 244  A	
53879	54035	Gl 411	NULL	
70667	70890	Gl 551	NULL	
71454	71681	Gl 559  B	
71457	71683	Gl 559  A	
87666	87937	Gl 699	NULL	
92116	92403	Gl 729	NULL	
118081	0	Gl  65  B	
 
Code Snippet 2:
var tool = new StackPanel();
tool.Children.Add(new TextBlock() { Text = "Proxima Centauri" });
tool.Children.Add(new TextBlock() { Text = "-1,538676906;-1,178494413;-3,752088504" });
 
var star = new Ellipse
{
    Height = 5,
    Width = 5,
    Fill = Brushes.Blue,
    RenderTransform = new TranslateTransform(-2, -2),
    ToolTip = tool,
    Cursor = Cursors.Hand
};
star.SetValue(Canvas.LeftProperty, 60.0);   //left-right distance from center in pixles
star.SetValue(Canvas.TopProperty, 20.0);    //top-bottom distance from center in pixles
star.SetValue(Canvas.ZIndexProperty, -500); //

starfield.Children.Add(star);
This code is not yet been made compatible with the above so it has absolute values instead of referring to variables.
Posted 24-Oct-12 10:59am
Comments
RavonX at 24-Oct-12 16:09pm
   
wouldn't you just need to create a new sub to handle starfields and send it a couple parameters from each datarow?
 
for instance:
 
foreach (DataRow row in table.Rows)
{
CreateStar(row.item(0), row.item(1), ...)
}
 
~~~~~~~~~~ then a new sub ~~~~~~~~~~
private void CreateStar(byval myinfo1 as object, byval myinfo2 as object, ...)
{
''' add code snippet 2 here and use parameters passed from the table rows
}

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 555
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 369
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,466
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,897


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 24 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100