Should i use SqlConnection or SqlDataAdapter Only ?
-
Should i use SqlConnection to connect to server before i use SqlDataAdapter to to fill the dataset ? Example : =============
string ConStr = "server=(local);uid=sa;pwd=password;database=NorthWind"; string cmdString = "select top 3 last name, firstname, title from employees"; SqlConnection conn = new SqlConnection(ConStr); SqlCommand com = new SqlCommand(cmdString,con); SqlDataAdapter da = new SqlDataAdapter(com); DataSet ds = new DataSet(); conn.open(); // Even without conn.open i stll able to fill the da. da.Fill(ds);
I try code above and i found that even without manuall open the connection i still able to fill up the dataadapter (da). So what is the best method to fill the dataadapter with out connection first out without using connection? -
Should i use SqlConnection to connect to server before i use SqlDataAdapter to to fill the dataset ? Example : =============
string ConStr = "server=(local);uid=sa;pwd=password;database=NorthWind"; string cmdString = "select top 3 last name, firstname, title from employees"; SqlConnection conn = new SqlConnection(ConStr); SqlCommand com = new SqlCommand(cmdString,con); SqlDataAdapter da = new SqlDataAdapter(com); DataSet ds = new DataSet(); conn.open(); // Even without conn.open i stll able to fill the da. da.Fill(ds);
I try code above and i found that even without manuall open the connection i still able to fill up the dataadapter (da). So what is the best method to fill the dataadapter with out connection first out without using connection?If your connection is closed already - the DataAdapter will open the connection, perform the query, and close the connection again. If you do not need the connection open for anything else then I recommend you let the DataAdapter do the clean up for you. [ADDITIONAL] I should add that if the connection is already open the DataAdapter will not close it [/ADDITIONAL] If you are going to perform a series of queries one-after-the-other then I would recommend that you open the connection, perform your queries, then close the connection. Does this help?
My: Blog | Photos | Next SQL Presentation WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More
-
If your connection is closed already - the DataAdapter will open the connection, perform the query, and close the connection again. If you do not need the connection open for anything else then I recommend you let the DataAdapter do the clean up for you. [ADDITIONAL] I should add that if the connection is already open the DataAdapter will not close it [/ADDITIONAL] If you are going to perform a series of queries one-after-the-other then I would recommend that you open the connection, perform your queries, then close the connection. Does this help?
My: Blog | Photos | Next SQL Presentation WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More