Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
566 views
in Technique[技术] by (71.8m points)

c# - 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;
    }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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);


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...