Hi! Thanks for trying to help me!

I'm building a program that will allow me to have a different form for each person in my database. When the program first starts, I want it to cycle through the database and open a form for each row (each person). I'm generally failing to understand the methodology for connecting directly to a DB to construct queries, and I'm still fuzzy on how DataRow works...

I'm using VisualStudio 2010 .Net4 and a MS SQL Server Compact DB (no password). The database is called Persons.sdf, the only table in the DB is called PeopleTable and the form that is filled with the info from the DB is called PersonBox.

Here is my code before I made a mess trying to figure out the DB connection (from Form1 - the form that holds all the PersonBox forms):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MaryAnne
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
 
                PersonBox childForm = new PersonBox(); 
                childForm.MdiParent = this; 
                childForm.Show(); 

        }

    }
}

And here it is after my 7th attempt at DB connections:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.SqlServerCe;


namespace MaryAnne
{
    public partial class Form1 : Form
    {

        private SqlCeConnection _conn;  //  Initializing the DB connection

        public Form1()
        {
            InitializeComponent();
            _conn = new SqlCeConnection(@"Data Source = |DataDirectory|\Persons.sdf");  //  Setting a new SQL Connection

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
            SqlCeCommand cmd = new SqlCeCommand();  //  All of this choses what to grab from the DB and then opens the connection
            cmd.Connection = _conn;
            cmd.CommandText = "SELECT [id] FROM PeopleTable";
            _conn.Open();
            SqlCeResultSet resultSet = cmd.ExecuteResultSet(ResultSetOptions.Scrollable | ResultSetOptions.Updatable);

            foreach (DataRow row in table.Rows) // Loop over the rows.
            {

                PersonBox childForm = new PersonBox(); 
                childForm.MdiParent = this; 
                childForm.Show(); 

            }

        }

    }
}

I've been doing SQL work since I was a kid (php, etc), but I've never had to do it with C# before. I am completely lost and all the information I've found elsewhere was really confusing. Please help!

Thanks!
-Mary Anne

Recommended Answers

All 2 Replies

Your connection string won't work as you don't specify where the database is. Take a look at this site, it has connection strings for everything :)

After using different parts from 3 tutorials I was able to construct a functioning db conn.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.