Click here to Skip to main content
16,009,150 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why we have to use using System.Data.SqlClient to work with Sql? I am asking this question because if we use using System, it should automatically include all namespaces which are under this System namespace. I am new in programming field. What I understand till now is there is System namespace and inside that there is Data namespace and further inside that there is one more namespace with the name SqlClient.

What I have tried:

I am learning dot net technologies and was trying to use Sqlconnection object in my program but it is showing error.
Posted
Updated 28-Aug-19 4:49am

Using "System" doesn't include everything under it. With no using statements you need to fully qualify the namespace so;

System.Data.SqlClient.SqlConnection


With "using System" it will prepend "System" to your classes to try and get a match, so this will work

Data.SqlClient.SqlConnection


With "using System.Data" this will work

SqlClient.SqlConnection


As you can see, it simply adds your various "using" snippets to the start of types that are not fully-qualified until it gets a hit. So if you want to use simply "SqlConnection" you'll need the full "using System.Data.SqlClient"
 
Share this answer
 
As long as your project references System.Data - and it should by default unless you have used an "odd" project template, then all you need to use is check the .CS file and make sure you have both of these lines at the top of the file:
C#
using System.Data;
using System.Data.SqlClient;
If those lines aren't showing any error (i.e. none of it is underlined in red) then you can use the SqlConnection, SqlCommand, and suchlike very easily:
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("SELECT Id, description FROM myTable", con))
        {
        using (SqlDataReader reader = cmd.ExecuteReader())
            {
            while (reader.Read())
                {
                int id = (int) reader["Id"];
                string desc = (string) reader["description"];
                Console.WriteLine("ID: {0}\n    {1}", id, desc);
                }
            }
        }
    }
If any of that code is underlined in red (except for the connection string in the first line) then you have a problem.

Other than that, you don't strictly need using lines for System.Data or System.Data.SqlClient: you can give the full class name every time you use them:
C#
using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(strConnect))
    {
    con.Open();
    using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("SELECT Id, description FROM myTable", con))
        {
        using (System.Data.SqlClient.SqlDataReader reader = cmd.ExecuteReader())
            {
            while (reader.Read())
                {
                int id = (int)reader["Id"];
                string desc = (string)reader["description"];
                Console.WriteLine("ID: {0}\n    {1}", id, desc);
                }
            }
        }
    }
Adding the using statements just reduces the typing and makes it more readable, is all.
 
Share this answer
 
Comments
Hassan Y. Ansari 29-Aug-19 11:14am    
Thank you so much both of you for reply. But my question is different. Suppose I declare one namespace with the name "namespace1" and inside this I declare one class with the name "HelloWorld". After that If I want to use "HelloWorld" class in my project later on so I have to use "using namespace1" statement. When I do it then I will be able to use "HelloWorld" class without using fully qualified name.
Now if I declare one more namespace with the name "namespace2" under the declaration of "namespace1". When I use "using namespace1" statement, it should automatically give access to "namespace2" also as after including namespace1, I will be able to use "HelloWorld" class. Both "HelloWorld" class and "namespace2" is declared under "namespace1" so why it is allowing access to only "HelloWorld" class and not to "namespace2" namespace.
OriginalGriff 29-Aug-19 11:43am    
It does - but only when you use the second namespace name to prefix the classes within it explicitly:
using Namespace1;
...
ClassInNS1 c1 = new ClassInNs1();
Namespace2.ClassInNS2 = new Namespace2.ClassInNS2();

If you think about it, if this worked:
using Namespace1;
...
ClassInNS1 c1 = new ClassInNs1();
ClassInNS2 = new ClassInNS2();

Then could it cause problems?
Yes, it could - very easily.
There are only so many "good names" for methods, properties, fields, and events - so it's pretty common to file a "Load" or "Save" method in a class for example. And class names themselves tend to be the same - different namespaces will often contain the same class names because they tend to do at least some of the same things.
If a using statement added every subclass (and their subclasses as well) then you'd increase the number of potential name duplications, and then you have to add the complete full name on more and more class usages.
And ... if it did, then when you see a class name, you don;t know where it is - it could be in NS1 or NS1.NS2 - you as the developer have to go and check to find out how it works.

By making "using" only "bring in" the single named namespace, you don't get that problems - if you want Namespace1 classes, you add that to the using list. If you also want Namespace1.Namespace2 classes, you explicitly add that.
Remember, "using" isn't necessary at all - you can delete all of them and use fully qualified names if you want - it's just there to make things a little easier for us all, not to be a "one-size-fits-all" universal solution!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900