-
query problem
hello.
my last problem about connecting to SQL was solved :D
is the code below correct for a query?
Code:
string conStr = "Data Source=.;Initial Catalog=WSD;Integrated Security=True"; //connection string
SqlConnection conn = new SqlConnection(conStr);
conn.Open();
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];
conn.Close();
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
Arian
-
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.