相关文章推荐
乖乖的韭菜  ·  HBuilderX.vue文件 以及 ...·  1 年前    · 
心软的香瓜  ·  Linker Tools Error ...·  1 年前    · 
腼腆的蚂蚁  ·  python 3.x - PyTorch ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

In my code, I am using OleDbConnection. When I refer to System.Data, it gives me the error that OleDbConnection is not found. But when I refer to System.Data.OleDb namespace then it runs fine. Why is this error occurring? I checked for the reference via add reference path for System.Data and it exist.

Find the below code for better clearance:

using System.Data;
public partial class Home : System.Web.UI.Page
  OleDbConnection oledbconn;
  protected void Page_Load(object sender, EventArgs e)

In above code, OleDbConnection oledbconn;---This line no 4 gives error that

Type or namespace OleDbConnection cannot be found

And now consider the following code,

using System.Data.OleDb;
public partial class Home : System.Web.UI.Page
    OleDbConnection oledbconn;
    protected void Page_Load(object sender, EventArgs e)

Here the code works fine,

I know that adding OleDb to System.Data will resolve my issue but I want to know why does it gives error in first place even if I have referred to System.Data namespace,

According to my knowing, when I refer to System.Data, it indirectly refer to all his child elements like OleDb, SqlClient and all..isn't it?

System.Data is the assembly you reference to in your project. It contains many namespaces within it, one of which is System.Data.OleDb.

If you right-click on the System.Data reference > View in Object Browser, you can browse for everything within the assembly. You can see in there that the OleDbConnection class is contained within the System.Data.OleDb namespace.

If you're unsure about where a class lives, always search in MSDN. This page would immediately tell you that OleDbConnection lives in:

Namespace: System.Data.OleDb

Assembly: System.Data (in System.Data.dll)

Please refer to the question that I have edited, I know that System.Data.OleDb exist in System.Data but my question is even System.Data should work...isn't it? – Reshma Feb 16, 2016 at 6:27 Unfortunately no. The using directive should be followed by a namespace, and namespaces are not nested. E.g. If you want to use both System.Diagnostics.Stopwatch and System.IO.File classes, you can not simply use using System. You have to specify both using System.Diagnostics; and using System.IO;. – NPras Feb 18, 2016 at 3:39

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.