locked
Retrieve Data from database

    Question

  • Well, i tried many times to create a database, and now i can do it, and insert values to the tables into visual c#!

    Nice, when i add the DataSet, it show the table correctly with the information, but i'd like to show all the names in a LISTVIEW, how can i get the names and list it in a listview
    Sunday, March 04, 2007 10:25 PM

Answers

  • Hi,

    You can get the data from database by datareader or dataAdapter as well, and then add columns and items in the listview.(you can regard items as rows in database)

    Here is the detail information about how to use listview control: http://msdn2.microsoft.com/en-us/library/6dwb14tw(VS.80).aspx

    If you have any further problems, please feel free to let me know.

    Thank you

    Monday, March 05, 2007 4:08 AM
  • Here is the sample code you can follow:

                SqlConnection conn = new SqlConnection

                    (...);

                SqlDataAdapter adt = new SqlDataAdapter("SELECT * FROM ...", conn);

                DataTable dt = new DataTable();

                adt.Fill(dt);

                foreach (DataRow drow in dt.Rows)

                {

                    ListViewItem item = new ListViewItem

                        (new string[]{drow[0].ToString(),drow[1].ToString(),drow[2].ToString(),...});

                    listView1.Items.Add(item);

                }

    If you have further problems, please feel free to let me know.

    Thank you

     

    Monday, March 05, 2007 4:55 AM

All replies

  • Hi,

    You can get the data from database by datareader or dataAdapter as well, and then add columns and items in the listview.(you can regard items as rows in database)

    Here is the detail information about how to use listview control: http://msdn2.microsoft.com/en-us/library/6dwb14tw(VS.80).aspx

    If you have any further problems, please feel free to let me know.

    Thank you

    Monday, March 05, 2007 4:08 AM
  • Here is the sample code you can follow:

                SqlConnection conn = new SqlConnection

                    (...);

                SqlDataAdapter adt = new SqlDataAdapter("SELECT * FROM ...", conn);

                DataTable dt = new DataTable();

                adt.Fill(dt);

                foreach (DataRow drow in dt.Rows)

                {

                    ListViewItem item = new ListViewItem

                        (new string[]{drow[0].ToString(),drow[1].ToString(),drow[2].ToString(),...});

                    listView1.Items.Add(item);

                }

    If you have further problems, please feel free to let me know.

    Thank you

     

    Monday, March 05, 2007 4:55 AM
  • sorry, but i want to know how to do it through this images !

    http://i15.tinypic.com/2j5nkmr.jpg

    http://i18.tinypic.com/2eatsnq.jpg

    http://i15.tinypic.com/2v9217a.jpg


    thanks for the other answer !
    Monday, March 05, 2007 10:11 PM
  • Hi,

    Here you have 3 columns: id, name, telephone

    So first add 3 columns to the listview their text should be id, name, telephone.

    Then you could put the similar code as

             SqlConnection conn = new SqlConnection

                    (...);

                SqlDataAdapter adt = new SqlDataAdapter("SELECT * FROM table1", conn);

                DataTable dt = new DataTable();

                adt.Fill(dt);

                foreach (DataRow drow in dt.Rows)

                {

                    ListViewItem item = new ListViewItem

                        (new string[]{drow[0].ToString(),drow[1].ToString(),drow[2].ToString()});

                    listView1.Items.Add(item);

                }

     

    into Form.load event.

     

    So what left for you is just to config connection and make it work.

     

    Otherwise, you can consider using datagrid or datagridview control in the toolbox.

     

    Thank you

    Tuesday, March 06, 2007 3:07 AM
  • There is any way to reduce this connection (i don't know how to make the connection, but this way works), i don't know where people will install the APP, then, i can't use the path "C:\....", but when i remove it, i got an error.

    "A database with the same name exists, or specified file cannot be opened, or it is located on UNC share."


    SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=\"C:\\Documents and Settings\\Miranda\\Meus documentos\\Visual Studio 2005\\Projects\\Agenda\\Agenda\\Database1.mdf\";Integrated Security=True;User Instance=True");

    Tuesday, March 06, 2007 4:40 PM
  • Hi,

    If you are not familiar with sql connection, why not use data source config wizard to help you in VS and you can also see the connection string it generate.

    Otherwise, try

    SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\Miranda\Meus documentos\Visual Studio 2005\Projects\Agenda\Agenda\Database1.mdf;Integrated Security=True;User Instance=True");

    Reference: http://www.eggheadcafe.com/articles/20060719.asp

    Thanks

    Wednesday, March 07, 2007 2:15 AM
  • i have a look at data source wizard, but when i try to add a data source to the listview, i don't have this option, but i have this option with the listbox, what should i do?
    Thursday, March 08, 2007 12:46 AM
  • Hi,

    Yes, you can take advantage to use data source wizard (regardless where it belongs to) just in order to get the connection string, copy it into the code as the value of the sqlconnection.

    Thank you.

    Thursday, March 08, 2007 2:32 AM