Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The type or namespace name 'SQLConnection' could not be found

Something seems wrong with the c# database for Accounts:

Right at the database I get the error message of:

Error 1 The type or namespace name 'Connection' could not be found (are you missing a using directive or an assembly reference?)

What am I possibly doing wrong with it? I would like to connect to the database in the Accounts

Table

Code 1:

 public void setCustAccounts(String custId) {          SQLConnection connect = acctsConnect();         Command statement = null;         ResultSet result = null;         String sql = "SELECT acctNo FROM Accounts Where Cid = '" + custId + "';";          try{             statement = connect.createStatement();             result = statement.executeQuery(sql);               while (result.next()){                 result.getRow();                 Account acct = new Account(result.getString("acctNo"));                 custAccounts.add(acct);                             }         }          finally {             connect.close();         }     } 

code:-

    public SQLConnection acctsConnect(){         try{             Class.forName("C:\\ChattBankMDB.mdb");         }catch(ClassNotFoundException e){             Console.WriteLine("Error: " + e);         }          SQLConnection connect = null;          try{             connect = DriverManager.getConnection("C:\\ChattBankMDB.mdb");         }catch(SQLException e){             Console.WriteLine("Error: " + e);         }          return connect;     }     } } 
like image 286
Hiy Avatar asked Apr 19 '15 17:04

Hiy


People also ask

What is the namespace for SqlConnection?

SqlConnection and SqlCommand are classes of a connected architecture and found in the System. Data. SqlClient namespace. The SqlConnection class makes a connection with the database.

How do I declare SqlConnection?

Creating a SqlConnection Object A SqlConnection is an object, just like any other C# object. Most of the time, you just declare and instantiate the SqlConnection all at the same time, as shown below: SqlConnection conn = new SqlConnection( "Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI");

What is System data SqlClient?

Represents a set of data commands and a database connection that are used to fill the DataSet and update a SQL Server database. This class cannot be inherited.


2 Answers

using System.Data.SqlClient; 

Along with the above line we need to also check if the actual system assembly reference is there or not. In my case I had the directive but assembly reference was missing.

To add assembly we can do the following.

Browse dll file for SqlClient in and add it.

--or--- simpler way is to install nuget package.

Right Click on Project > Manage Nuget Packages > Search & install 'System.Data.SqlClient'

Make sure it is compatible with the type of project (Core/Standard);

like image 173
Morse Avatar answered Oct 09 '22 03:10

Morse


You are missing the using Directive statement in your code.

using System.Data.SqlClient; 
like image 35
Black Frog Avatar answered Oct 09 '22 01:10

Black Frog