Thread: query problem

  1. #1
    Registered User
    Join Date
    Nov 2003

    query problem

    my last problem about connecting to SQL was solved
    is the code below correct for a query?

    string conStr = "Data Source=.;Initial Catalog=WSD;Integrated Security=True";                                                      //connection string
    SqlConnection conn = new SqlConnection(conStr);
    string query = "select * from [P_E_wsd] where (english='network')";                                            //query
    DataSet ds = new DataSet();
    SqlDataAdapter da = new SqlDataAdapter();
    da.SelectCommand = new SqlCommand(query, conn);
    da.Fill(ds, "P_E_wsd") ;          
    DataTable dt = ds.Tables[0];
    how can I access to the result of the query?
    I know the answer should be in 'dt' but after the code is run I see non of the entry of the table in 'dt'(I check it in locals) while if I use that query command in SQL the answer will be a table with 2 rows and 4 columns.

    thank you

  2. #2
    Registered User
    Join Date
    May 2006
    You don't need to pass the table name to the Fill() method if you're just going to access it by index. Conversely, if you do pass that argument, you can then access the table with ds.Tables["P_E_wsd"] instead of by index. Also, if your code throws an exception will conn.Close() be called? You'll want to put that in a finally block or end the connection by putting it into a using block. If that same query returns 2 rows when run from SQL on the same host/db with the same credentials, then dt.Rows.Count should be 2. You can look at the table in debug mode by hovering over it, then clicking the magnifying glass.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. Problem in when I run insert query
    By Bargi in forum Windows Programming
    Replies: 1
    Last Post: 05-09-2007, 10:46 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM