Connection to an instance-specific SQL Server DB from a Rails Application - sql

We are connecting to a SQL Server application from a Rails 4 application running on Heroku. Everything used to work fine. We used Proximo to give us a static IP address we could then allow through our Firewall. A simple ENV variable was used on heroku to store the SQL Server request URI like so:
# Gemfile
gem 'activerecord-sqlserver-adapter'
gem 'tiny_tds'
# customer.rb
class Customer < ActiveRecord::Base
establish_connection(ENV["MS_SQL_DATABASE_URL"])
..
end
MS_SQL_DATABASE: sqlserver://username:password#176.32.98.166/DATABASE1?autocommit=true&timeout=5000
Now this all works fine, until the database was updated to a newer version and Database1 now moved to a new instance called Instance1. So in order to connect to the upgraded database we need to specify an instance name as part of our connection uri.
Specifying the instance name works locally when we VPN into the network and use the following connection URL
MS_SQL_DATABASE: sqlserver://username:password#192.168.168.1/DATABASE1?autocommit=true&timeout=5000&dataserver=192.168.168.1%5CINSTANCE1
Note we are using the dataserver property and specifying the full path to the server. The server name inside the network is known as 192.168.168.1 and also as server1. But from outside the network (on Heroku) we cannot address it like that.
What will be the proper way to address the server name to specify the instance name to allow a connection from heroku?
Is it even possible to do this? If not, what are some alternatives?

Related

Connecting to different servers in SQL Server

Scenario- We have more than 100 stores and each store has a server. I've created one application to install each users desktop. Their system needs to be connected with their store server, we have local database in place with the application which has procedures which will be interacting with the server. I can't hard code the server name in each procedures and in the SQL, Using DBLink didn't work.
What is the best way to achieve this?
At login page/form in addition to Username and Password, add Server field.
Your app can list any available sql servers on the network if there is only one then connect to it. If more than one put a dialog up to the users to confirm.
You can use:
sqlcmd -L
That will return any visible sql servers
You have to say some way to the Application installed ,which server to connect to..
there are many approaches...
1.Store connection details in a table from which the application can read to like below
storeid connectionstring
1 somestring
So application at startup event,figure out which connection string to connect to..This approach would not require recompiling app and installing it everytime whenever store or connection string changes
2.In WebConfig,store connection details as key value pair like below..
<add key="storeid1" value="server=localhost;database=myDb;uid=myUser;password=myPass;" />
and then connect based on storeid,but this requires change in config file and app installation ,whenever a connection string or Storeid changes

How to query a remote SQLAnywhere16 db using Rails?

I am trying to set up a Rails app to query a remotely hosted SQLAnywhere 16 db and to display some query information as HTML. I'm using current versions of Ruby and Rails on Mac El Capitan. The remote server is running Windows. The server is located in my building and I am on the same LAN, so if there's a simpler solution that involves accessing the box itself physically I'm open to that.
On my Windows machine I am able to connect to the server and do queries on the db using a SQL client.
I have sqlanywhere and activerecord-sqlanywhere-adapter gems installed. I also have the SQL Anywhere 16 client installed on my (Mac) machine. The remote db set up in my database.yml file as another db:
my_external_database:
adapter: sqlanywhere
encoding: utf-8
username: (username)
password: (password)
server: (name of server)
database: (db name)
host: (IP of server)
I'm calling it like so:
class MyExternalModel < ActiveRecord::Base
establish_connection(:my_external_database)
self.table_name = '<a particular table in the db>'
self.primary_key = 'primarykey'
end
When I go into the rails console and do MyExternalModel.first to test the connection, I get:
LoadError: Could not load SQLAnywhere DBCAPI library
After doing some googling, this seems to be a problem with configuring SQLAnywhere. However, I believe I configured it properly (in bash using bin64 config), like so:
. /Applications/SQLAnywhere16/samples/sample_config64.sh
Enter destination directory for copy of the samples [/Users/(user)/sa16_samples]:
Copying samples...
Done
Setting up data sources...
SQL Anywhere Data Source Utility Version 16.0.0.2041
Configuration "SQL Anywhere 16 Demo" written to file /Users/(user)/.odbc.ini
SQL Anywhere Data Source Utility Version 16.0.0.2041
Configuration "SQL Anywhere 16 CustDB" written to file /Users/(user)/.odbc.ini
Done
Setting up sample_env script...
Done
This is as far as I'm able to get. Any recommendations are greatly appreciated!
I use SQLAnywhere for a production server and it works fine using the very same gem you're using. I use a Mac for development and my database is running in Windows (or Linux).
In order to get rid of this error, try the command (on your Mac):
# adjust SQLAnywhere to your version -> 16
source /Applications/SQLAnywhere12/System/bin64/sa_config.sh
I'm using Rails 4 and SQLAnywhere 12 (but tested with 16 and it works too).
After that, try to load console (rails c) and do some tests there.

Windows Application ignores app.config and uses something to connect to local database

The application sits on a virtual environment and when I remote in and run the application, it connects to the remote database. However, when I remote in with a service account and double click the same .exe, it tries to connect to the local host database and ignores the app.config. The code is the same, only the login name I use is different. The login I use is part of the local admin group. Any ideas?
You haven't indicated whether or not this is the case in your question, but my first suspicion is that you are storing the connection strings in settings, but the connection string has been marked as a user-specific setting.
In the logic of the code it was doing a comparison of the SQL server setting in the config (Settings), which was entered in lower case, against the list of SQL servers (all in upper case). Since it couldn't find any match, the datasource was blank [datasource=;],hence causing the code to look local. My fix was to use String.Compare and ignore the case, which created the match and I was able to connect to the remote SQL server.

ODBC Connection String Problem

I am having major trouble connecting to my database via ODBC.
The db is local (but I have a mirror on a virtual machine), so I am trying to use the connectionstring:
Dsn=MonetDB;host=TARBELL
where TARBELL is the name of my computer. However, it doesn't connect. BUT, this string does:
Dsn=MonetDB;host=localhost
as does
Dsn=MonetDB
Can anyone explain this? I am at a complete loss.
I have taken down my firewalls (at least until I get this figured out), so that can't be the problem.
I eventually want to change the TARBELL to the mirrored virtual machine running another instance of the database.
Many thanks,
Brett
I can recommend connectionstrings.com for details on the supported syntax across all of the supported ADO.NET providers.
DNS usually resolves TARBALL and localhost differently. You can see with ping:
c:\>ping tarball
Pinging tarball [192.168.1.99] with 32 bytes of data:
^^^^^^^^^^^^
c:\>ping localhost
Pinging tarball [127.0.0.1] with 32 bytes of data:
^^^^^^^^^
The computer name resolves to the external IP, while localhost resolves to the special IP 127.0.0.1 that always points at the local machine. Some installations of MySQL listen on localhost only, so if you specify the computer name, they stop listening.
This behaviour is configured using the bind-address option:
--bind-address=127.0.0.1
Or the equivalent (MySQL also uses DNS to resolve hostnames):
--bind-address=localhost
To make the server listen on all interfaces, specify:
--bind-address=0.0.0.0
On Windows, MySQL reads configuration options from:
WINDIR\my.ini, WINDIR\my.cnf
C:\my.ini, C:\my.cnf
INSTALLDIR\my.ini, INSTALLDIR\my.cnf
See the MySQL manual pages for some more information.
figured i'd post an answer to this here since i found this while searching for a windows monetdb connection string. this is the conn string i'm using and is working for monetdb 5:
DRIVER=MonetDB ODBC Driver;PORT=50000;HOST=<your host>;DATABASE=<your db>;UID=monetdb;PWD=monetdb
I've never seen a "host" parameter for a DSN type connection string. DSN's are either stored with the user, the system or as a file. The way you have referenced your DSN here, it is either stored under your user account or with the system. With a DSN, all the credentials and information about which server and driver to be used are stored in the DSN. If you want control over those parameters, you should consider a DSN-less connection string like so:
"Driver={Mysql}; Server=[server_name];Port=[port_number]; Database=[database_name];UID=[username]; PWD=[password]"

Installing SQL Server Express 2005 - but it already exists on the machine

I built an application for a client that requires SQL Server 2005 Express. Everything worked fine on all the machines except for one. On this one they already had an application that uses SQL Server Express, so it was already installed on the machine, but nobody knows which application uses it or any usernames/passwords.
Can I simply install another copy into a different folder? This just doesn't seem right to me, and I know this has to be a common scenario. Can someone point me in the right direction on how I should proceed?
Thanks!
Darvis
Yes you can just install into a different directory, as a new "named instance" of SQL Server Express.
To install, follow Step 8 on Microsoft's Install How-To:
On the Instance Name page, select a Default instance or a Named instance for your installation. If you select Default instance, an existing default instance will be upgraded. If you select Named Instance, specify an instance name
So what you need to do is specify the Named Instance and specify your own instance name, and connect to it using the URL format as above.
As the Microsoft How-To mentions, the default installation is a named instance as well, with the name "SQLExpress", which is why if you want to stop or start the service with net start or `net stop' you need to write something like:
net start mssql$sqlexpress
and the hostname part of the connection string for a default SQL named instance is:
.\SQLEXPRESS (or localhost\SQLEXPRESS)
You should be able to log into it using Integrated Windows Authentication using an administrator type account on the PC, and use that to reset passwords on any SQL server type logins.
Failing that, yes, you should be able install a "named instance". You connect to it by supplying "hostname\instancename" as the server name.
In all likelihood, the culprit is Outlook's Contact Manager.
You should just uninstall the "feature". If you can't, you can create an additional instance of SQL Express, which you can access as COMPUTERNAME\INSTANCENAME.