1

I have a question of how to use System.Data and System.Data.SqlClient.

In the following example I've used System.Data.SqlClient namesspace. Can I write System.Data instead of System.Data.SqlClient here, because SqlClient is already included in the System.Data namespace?

My code:

using System;
using System.Data.SqlClient;

class Program
{
    static void Main()
    {
    // First access the connection string.
    // ... This may be autogenerated in Visual Studio.
    string connectionString =
                     ConsoleApplication1.Properties.Settings.Default.ConnectionString;
    //
    // In a using statement, acquire the SqlConnection as a resource.
    //
    using (SqlConnection con = new SqlConnection(connectionString))
    {
        //
        // Open the SqlConnection.
        //
        con.Open();
        //
        // The following code uses an SqlCommand based on the SqlConnection.
        //
        using (SqlCommand command = new SqlCommand("SELECT TOP 2 * FROM Dogs1", con))
        using (SqlDataReader reader = command.ExecuteReader())
        {
          while (reader.Read())
          {
            Console.WriteLine("{0} {1} {2}",
            reader.GetInt32(0), reader.GetString(1), reader.GetString(2));
          }
        }
    }
  }
}

1 Answer 1

2

If I understand you correctly then no. You have to be explicit with namespaces. If you wanted to access DataTable in your example you would need to include System.Data - but that wouldn't give you access to any of the nested namespaces.

From here

Create a using directive to use the types in a namespace without having to specify the namespace. A using directive does not give you access to any namespaces that are nested in the namespace you specify.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.