Perl Connect to MSSQL Database Engine [duplicate] - sql-server

This question already has answers here:
How do I connect to a MSSQL database using Perl's DBI module in Windows?
(4 answers)
Closed 7 years ago.
I am new to SQL and I am trying to create a Perl script to run a simple SELECT statement on a MSSQL Database I have access to.
The database I am connecting to authenticates with SQL Server Authentication. I found this but I don't think it applies because it is for ODBC.
Any help would be appreciated

The link you referenced is absolutely the right track. You'll be using an ODBC connection to talk to MS SQL Server. Otherwise, you can use DBD::Sybase to connect (SQL Server grew out of the Sybase code-base, so they share the underlying protocol, which is why the drivers for Sybase can be used).
I would recommend using Microsoft's official ODBC driver with DBD::ODBC over DBD::Sybase.

You didn't find any useful links?
I'm using MSSQL server for DB connection as below:
my $dbh = DBI->connect( "DBI:Sybase:database=DBNAME;server=DBSERVER",
"User", "Password", { PrintError => 1, AutoCommit => 1 } )
or die "Cant connect to database: $DBI::errstr\n";
$sth = $dbh->prepare("select * from Table");
$sth->execute();
$value = $sth->fetchrow_array();

Related

How do I resolve encryption error connecting to SQL Server from Oracle SQL Developer

My objective is to use the migration tool in SQL Developer to migrate 2 SQL Server databases to Oracle. I am attempting to connect to a SQL Server database at a remote location using Oracle SQL Developer 4.0.
I have installed jtds.1.3.0.jar per instructions, and get the SQL Server and Sybase tabs in the connection properties window.
I have entered the user, pw, hostname, port, and database name. When I try to Retrieve Database, or connect, I get the error message:
Status : Failure -Test failed: I/O Error: DB server closed connection.
The SQL Server DBA tells me her error logs contain the following error message:
Encryption is required to connect to this server but the client library does not support encryption; the connection has been closed. Please upgrade your client library. [CLIENT: (my computer's IP address)] Error: 17835, Severity: 20, State: 1.
When I look at SQL Server documentation about this error message, it talks about certificates and SQL Server Connection Manager. Some relevant information to show that I've covered the basics:
Using the same connection parameters, I am able to connect to the SQL Server database using Toad for SQL Server.
Using the same connection parameters, a DBA set up an Oracle Transparent Gateway, and I can connect to the SQL Server database using the gateway and a database link. (As far as I know, neither of these methods required a certificate to connect.)
Both of these use ODBC; SQL Developer uses jdbc with TCP/IP.
The DBA assures me TCP/IP connection is turned on for the SQL Server databases.
I have gone into the XML file where SQL Developer stores the connection string. It has the form jdbc:jtds:sqlserver://sql-xxx-xxx.mw.nos.xxx.com:1433/my_db, which looks correct. I have tweaked it by adding the instance name, but no difference. The database has a default instance name, anyway, so it shouldn't be necessary.
A co-worker has been able to connect to a local SQL Server database on the same machine where SQL Developer was running (both on his laptop).
As a workaround, I am also pursuing the possibility of installing SQL Server on a local workstation, and copying the databases there for processing, but I don't know if I can get approval. If you know another tool for migrating from SQL Server to Oracle, please also let me know.
Thanks in advance.

Error when trying to create new database table in SQL Server 2016 from csv file while using python 3.5 with pandas and sqlalchemy

Problem
I am trying to use python to read my csv file and put it into Microsoft SQL Server 2016 as a new table. Simply put, I don't want to create a table on SQL and import the csv, I want to write a script in python that can read the csv and create a new table in SQL for me.
UPDATE
I may have to rethink my approach. I corrected the driver, but I am getting the following error from to_sql. I am thinking that there is something wrong with my authentication scheme. Sadly, the to_sql documentation and sql_alchemy is not shedding much light. Starting to consider alternatives.
sqlalchemy.exc.DBAPIError was unhandled by user code
Message: (pyodbc.Error) ('08001', '[08001] [Microsoft][SQL Server Native Client 11.0]Named Pipes Provider: Could not open a connection to SQL Server [53]. (53) (SQLDriverConnect)')
Code
import pandas as pd
import sqlalchemy
#Read the file
data = pd.read_csv(file.csv)
#Connect to database and write the table
server = "DERPSERVER"
database = "HERPDB"
username = "DBUser"
password = "password"
tablename = "HerpDerpTable"
driver = "SQL+Server+Native+Client+11.0"
#Connect to SQL using SQL Server Driver
print("Connect to SQL Server")
cnxn = sqlalchemy.create_engine("mssql+pyodbc://"+username+":"+password+"#"+server +"/"+database + "?driver="+driver)
UPDATE
I rewrote the string as follows, but it doesn't work:
sqlalchemy.create_engine('mssql+pymssql://'+username+':'+ password + '#' + server + '/' + database + '/?charset=utf8')
data.to_sql(tablename, cnxn);
Attempts
These are some important things to note in my approach. Pay special attention to the second bullet point I share below. I think my connection string for create_engine is somehow or maybe wrong, but don't know what is wrong because I followed the documentation.
I believe I am in a DSN-less situation. Thus, was attempting to connect by other means as described by the documentation.
I was using this link to help me create the connection string part in create_engine.
I tried to_sql to write the to the database, but think my connection string might still be messed up? I consulted this question on stackoverflow.
Update I added the driver specification as MaxU and the documentation for sqlalchemy specified. However, I am getting an error saying my data source name was not found and no default driver is specified with to_sql. Do I need to feed to_sql the driver as well? If so, where is the documentation or a sample code that shows me where I am going wrong?
I am making good effort to pick up python and to use it as a scripting language because of future goals and needs. I would appreciate any assistance, help, mentorship rendered.
You should explicitly specify the MS SQL Server driver if you use SQLAlchemy version 1.0.0+:
Example:
engine = create_engine("mssql+pyodbc://scott:tiger#myhost:port/databasename?driver=SQL+Server+Native+Client+10.0")
Changed in version 1.0.0: Hostname-based PyODBC connections now require the SQL Server driver name specified explicitly. SQLAlchemy
cannot choose an optimal default here as it varies based on platform
and installed drivers.
List of Native SQL Server Client Names
SQL Server 2005:
SQL Server Native Client
SQL Server 2008:
SQL Server Native Client 10.0
SQL Server 2016:
SQL Server Native Client 11.0
Alternatively you can use pymssql:
engine = create_engine('mssql+pymssql://<username>:<password>#<freetds_name>/?charset=utf8')

Connect AnyLogic to SQL Server database

the new build-in database in AnyLogic 7.3 allows to connect to a SQL Server database. I am struggling to connect it to a local database that I set up using SQL Management Studio (using SQL Server 2008 R2).
A few more facts to clarify:
My dbase is called "myDBase" and has a db_owner called "myNewLogin".
I log into SQL Management Studio using "Server type" = Database engine. The Server name is "NAMEOFMYMACHINE\SQLEXPRESS". I set "Authentication" to Windows authentication
my login "myNewLogin" is set to use "SQL Server authentication" in its properties. It uses "myPassword" as a password (but doesn't enforce it)
I set up the AnyLogic import wizard as below:
I have tried to use different inputs (such as "sa" for the Login,...) but in any combination, I always get the error
"Login failed for user ..."
I have also turned off my Windows firewall to allow port 1433 communication, but the result is the same.
Have you been able to load from a local SQL Server database? How did you set it up, which versions did you use... The more specific your reply, the better.
thx a lot in advance
Thanks again for your help.
I fixed the problem by installing SQL Server 2014 (was 2008) and logging in using "Mixed Method" with a "sa" account.
have you tried host: localhost\sqlexpress ?

Connecting to SQL Server using PHP, ODBC, and Windows authentication

I'm trying to use PHP to connect to an ODBC data source using Windows authentication. I can connect just fine to the server in SQL Server so I know it's running. When I try to run the command
$link = odbc_connect("my_odbc","","");
I get the error:
"Warning: odbc_connect(): SQL error: [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user ''., SQL state 28000 in SQLConnect in C:\Users..."
I tried:
$link = odbc_connect("Driver={ODBC Driver 11 for SQL Server};
Server='my_odbc';Integrated Security=SSPI","","");
Which returned the message:
Warning: odbc_connect(): SQL error: [Microsoft][ODBC Driver 11 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [53]. , SQL state 08001 in SQLConnect in C:\Users..."
Not sure what I'm doing wrong.
my_odbc is a SQL Server (2008) on a different machine. I don't have admin privileges on that database so I can't change anything on that end (such as enabling SQL Server authentication).
I am running Windows 7 and using PHP Version 5.6.12
phpinfo() indicates that ODBC Support is enabled as well as pdo_sqlsrv support
Maybe you should try PDO (the performance difference isn't that great) with SQLsrv plugin (this what I'm using to connect to my other boss' software which use SQL Server 2008 database):
$connection = new PDO("sqlsrv:Server=" . $this->sourceServer . ";Database=" . $this->sourceDB, $this->sourceUser, $this->sourcePW);
$connection->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
You can download the plugin here
https://www.microsoft.com/en-ca/download/details.aspx?id=36434
In Xampp, you must copy the dll in that folder:
C:\xampp\php\ext
And you must add that line to your php.ini
extension = php_pdo_sqlsrv_56_ts.dll
Note: Don't forget to restart your server so it can take in account the new php.ini file.
Let me know if it works for you
This error is a General Error that arises due to unable to establish connect to the server for some reasons. Its important to know what server are you using with PHP and the application used with it. Example Apache, Xampp or Wamp etc.
Here are somethings you could try like.
Let me brief, below are the reference links.
Check whether it pings
Enable TCP/IP connection SQL Server Configuration if not enabled. To do so, Open SQL Server Configuration Manager -> SQL Server Network
Configuration->Protocols for SQL server->TCP/IP(set to enabled).
Restart SQL services.
Enable Remote Connection from server
Open the Port, To do so Windows Firewall Settings-> Exceptions -> add a Port (Name:SQL;Port:1433;TCP) then from Exceptions tick SQL and save.
Enable running browser services.SQL Server Configuration Manager -> SQL Server Services - > SQL Server Browser set to running.
defining Port in connection string
add instance name with machine name if more than one instance is being used
sample
$user = 'username';
$pass = 'password';
//Use the machine name and instance if multiple instances are used
$server = 'serverName\instanceName';
//Define Port
$port='Port=1433';
$database = 'database';
$connection_string = "DRIVER={ODBC Driver 11 for SQL Server};SERVER=$server;$port;DATABASE=$database";
$conn = odbc_connect($connection_string,$user,$pass);
here are useful links that i found regarding the issue
resolving could not open a connection to sql server-errors
sql server provider named pipes provider error
Have a look at these links too
Named Pipes Provider: Could not open a connection to SQL Server [53]
Someone had the same error message and asked and answered his own question here.
To quote...
I´m sorry for the troubles.
The problem was, that I was using SQL Server Native Client 11.0 as driver. I switched it to SQL Server and now it works :/
Hopefully this at least helps someone, being in a similar problem....
A significant omission from your question is how you are running PHP and with which credentials.
Assuming you are using IIS or Apache (or another web server), then your PHP process is probably running under the local system account:
Since this account is local it doesn't have any authorisation to access your remote SQL Server.
You could alter the IIS/Apache service to run with the credentials (yours?) that are authorised to connect to SQL Server, but be aware this might cause other permissions issues with the service as well as being a problem if you change your password in the future.
Try running a test script from the command line (which should run with your credentials by default) to determine where the problem is:
php -r "var_dump(odbc_connect(...));"
Look at the regedit HKLM/SOFTWARE/ODBC
what is your folder name ?
If there is a "SQL Server Native Client 11.0" then you have to write your php code like
odbc_connect("Driver={SQL Server Native Client 11.0};Server=ip;Database=db;", "user", "pass");
However some of the server like server 2008 will be record this odbc like
ODBC Driver 11 for SQL Server
Then you have to replace SQL Server Native Client 11.0 by ODBC Driver 11 for SQL Server.
You have to create an ODBC interface first. In principle, this should also work without a user and pw. If defined in interface.
$pdo = new PDO('odbc:dsnName','user','pw');
Enable in IIS on Manager PHP o PDO_ODBC:
I have authenticated by Windows with following PHP statement:
$ Conn = new PDO ("odbc: Driver = {SQL Server}; Server = JAMILI-PC \ SQLEXPRESS; null; null");
I am using Windows 2008.
I hope it solves your problem.

How do I access a SQL Server database from a Perl script in Linux?

I have a Perl script on a Linux (Ubuntu 8.10) machine and I need to write data to a SQL Server Database. I've been trying to use the DBD::ODBC module but I can't get it to connect. Where can I get a free/open source driver to use to use for the ODBC connection or is there another way to do this from Perl on Linux?
I connect to SQL Server 2005 with the stack of unixODBC, freeTDS (this is the driver) and DBD::ODBC.
After you install these components, edit /etc/unixODBC/odbc.ini to read like this:
[DNS]
Description = my database
Driver = /usr/lib/libtdsodbc.so #path to freeTDS driver
Server = ServerName
Database = DatabaseName
Port = 1433 #sql server default port
TDS_Version = 9.0 #9.0 is sql server 2005
try domain login = yes
try server login = yes
nt domain = DOMAIN
If all goes well, you should be able to connect with:
$dbh = DBI->connect('dbi:ODBC:DNS', "userName", "passWord");
Good luck!
Use the DBD::Sybase module, at one point Sybase and MS SQL Server shared a common codebase.
You may also want to investigate the open source FreeTDS libraries. See the FreeTDS FAQ Question "Which Perl library should I use".

Resources