Cannot open a SQL server database

Viewed 257

I am using SQL Server to connect to the database. But, I receive this error when I attempt to connect.

Code for connecting database

ConnectionConfig connectionConfig = new ConnectionConfig();
Connection con = connectionConfig.CONN();

The error produces by Log

Cannot open database "FBMain" requested by the login. The login failed.

Here is the code

public class ConnectionConfig {
    private String ip = "192.168.56.1";
    private String classs = "net.sourceforge.jtds.jdbc.Driver";
    private String db = "FBMain";
    private String un = "sa";
    private String password = "Password123";

    @SuppressLint("NewApi")
    public Connection CONN() {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        Connection conn = null;
        String ConnURL;
        try {
            Class.forName(classs);
            ConnURL = "jdbc:jtds:sqlserver://" + ip + ";"
                + "databaseName=" + db + ";user=" + un + ";password="
                + password + ";";
            conn = DriverManager.getConnection(ConnURL);
        } catch (Exception e) {
            Log.e("ERROR", e.getMessage());
        }
        return conn;
    }
}

I have checked the user mappings for user sa. The database role membership for FBMain is set to db_owner and public. But, I can't connect at all.

1 Answers

There are TWO instances od SQL Server on 192.168.56.1, default and named(SQL).

The app tried to connect to the default instance where there is no FBMain database. This database exists on the named istance.

To connect to the named instance the connection string should specify the port (that can be found in SQL Server error log or in the Configuration Manager), or it can specify instance name like this: 192.168.56.1\SQL or ANIME-PC\SQL and SQL Server Browser shuold run in the second case.

Related