0

My android application is supposed to get data from an Azure SQL database, but the queries keep responding with

Reference to database and/or server name is not supported in this version of SQL Server

On the server side audit log it just states

Reference to database and/or server name in 'delivery.dbo.upload' is not supported in this version of SQL Server

Code:

public class CheckDeliveries extends AsyncTask<String, String, String> {

        String z = "";
        Boolean isSuccess = false;
        private ArrayList<ModelU> arrayList;


        //@Override
        protected void onPostExecute(String r) {
            if (z == "ok") {
                GetDeliveries adapter = new GetDeliveries(ViewUpcoming.this, arrayList);
                mRecyclerView.setAdapter(adapter);
            }
            Context context = getApplicationContext();
            CharSequence text = "2 " + z;
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
        }

        @Override
        protected String doInBackground(String... strings)
        {
            try
            {
                con = connectionclass();
                if (con == null)
                {
                    z = "Internet Issue";
                }
                else
                {
                    String query = "SELECT * FROM [delivery].[dbo].[upload]";
                    Statement stmt = con.createStatement();
                    ResultSet rs = stmt.executeQuery(query);
                    z = "ok";

                    if(rs.next()) {
                        do {
                            @SuppressLint("Range") ModelU modelu = new ModelU(
                                    "" + rs.getInt(rs.getInt("ID")),
                                    "" + rs.getString(rs.getInt("Name")),
                                    "" + rs.getString(rs.getInt("Address")),
                                    "" + rs.getString(rs.getInt("Status"))
                            );
                            arrayList.add(modelu);
                        }
                        while (rs.next());
                    }
                    else {
                        z = "Invalid Query";
                        isSuccess = false;
                    }
                }
            } catch (Exception ex) {
                isSuccess = false;
                z = ex.getMessage();
            }

            return z;
        }
    }

    @SuppressLint("NewApi")
    public Connection connectionclass() {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        Connection connection = null;
        String ConnectionURL = null;
        try {
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
            ConnectionURL = "jdbc:jtds:sqlserver://2033051.database.windows.net:1433;database=delivery;user=XXXXXXXX;password=XXXXXXXX;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;ssl=request;loginTimeout=30;";
            connection = DriverManager.getConnection(ConnectionURL);
        }
        catch (SQLException se)
        {
            Log.e("error 1", se.getMessage());
        }
        catch (ClassNotFoundException e)
        {
            Log.e("error 2", e.getMessage());
        }
        catch (Exception e)
        {
            Log.e("error 3", e.getMessage());
        }
        return connection;
    }
} 

Toastbox output

Azure database

Any help is greatly appreciated!

jarlh
  • 42,561
  • 8
  • 45
  • 63
Jialat
  • 1

1 Answers1

0

This error occurs because Azure does not allow to alter the master database. To resolve this, you need to connect directly to the Azure database you are going to use.

Delete the SQL Azure linked server that we created and create a new one.

enter image description here

Under the Server type section of the General tab, choose the Other data source radio button. The name for the linked server in the Linked server text box can be, this time, whatever you like (e.g. AZURE SQL DATABASE). Under the Provider drop down box, choose the Microsoft OLE DB Provider SQL Server item. In the Data source text box, enter the name of the SQL Azure (e.g. server.database.windows.net). The most important setting in order to correctly create a linked server to an Azure SQL database is to enter the name in the Catalog text box (e.g. TestDatabase) of an Azure SQL database for which you want to create a linked server to an Azure SQL database. Otherwise, if this field is left empty, we will encounter the same 40515 error when trying to get a list of the tables under the Catalogs folder.

Refer this article by Marko Zivkovic

Abhishek K
  • 3,047
  • 1
  • 6
  • 19