简体   繁体   中英

Getting data from each row in a SQL Server CE database

I have done this before, but for the life of me I can't remember how this worked.

I have a database that has a bunch of rows with data in them like names and ID numbers. What I need to do is populate a treeview from names in the database. I am running up against an issue just getting the reader to read multiple rows in the database. It only seems to be reading the first row and not subsequent rows. the actual task would be similar to below :

For each row in database add a parent node to treeview where the name is = to ( reader[4].ToString() ). That's about it. At the moment all I am trying to do is just get it to pop a messagebox showing that it's reading the multiple rows.

Please what am I missing to get this working?

SqlCeConnection conn = null;
    try
    {
        using (conn = new SqlCeConnection("Data Source =" + ConfigurationFile + "; Password =*********"))
        {
            conn.Open();
            SqlCeCommand cmd = conn.CreateCommand();
            cmd.CommandText = "select * from t_mainprofiles";
            cmd.ExecuteNonQuery();
            var reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                ID = (Convert.ToInt32(reader[1]));
                profileID = (Convert.ToInt32(reader[2]));
                profileNAME = (reader[4].ToString().Trim());
                profileLOC = (reader[5].ToString().Trim());
                profileCHILD = (reader[6].ToString().Trim());
            }
            MessageBox.Show(profileNAME);
            reader.Close();
        }
    }
    catch(Exception error)
    {
        MessageBox.Show(""+error);
        System.Diagnostics.Process.GetCurrentProcess().Kill();
    }
    finally
    {
        conn.Close();
    }

Try removing the line cmd.ExecuteNonQuery();

Here is an example from MSDN http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.read(v=vs.110).aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM