Laravel Config ODBC Driver - sql-server

I need to config Laravel 4 to use the ODBC PDO Driver for SQL Server 2000
I have tested it in a plain PHP file and it works perfectly, However, I can't get the right config inside Laravel.
This is my connection string >
$conn = new PDO("odbc:Driver={SQL Server};Server=MyHOST;Database=myDb;User Id=sa;Password=;");
I got this so far in the Laravel config/database.php
Edit
Ok, i followed the instructions from https://github.com/ccovey/odbc-driver and i configured:
I changed it to
'odbc' => array(
'driver' => 'odbc',
'dsn' => 'Driver={SQL Server};Server=MyServer',
'grammar' => 'SqlServerGrammar',
'username' => 'user',
'password' => 'pass',
'database' => 'staPPM',
),
Im getting an error FatalErrorException, Grammar not Found

Laravel 4 still does not support ODBC directly, you'll have to do it yourself or you can try to use this driver: https://github.com/ccovey/odbc-driver.
You'll have to add connection that should look something like:
'odbc' => array(
'driver' => 'odbc',
'dsn' => 'Driver={iSeries Access ODBC Driver};System=my_system_name;',
'grammar' => 'DB2',
'username' => 'foo',
'password' => 'bar',
'database' => '',
'grammar' => 'SqlServerGrammar',
),
As noted in the package docs, you have to provide a valid DSN to connect to your server: those ones are examples of valid connection strings:
"Driver={SQL Server};Server=(local);Trusted_Connection=Yes;Database=AdventureWorks;"
"Driver={Microsoft ODBC for Oracle};Server=ORACLE8i7;Persist Security Info=False;Trusted_Connection=Yes"
"Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\bin\Northwind.mdb"
"Driver={Microsoft Excel Driver (*.xls)};DBQ=c:\bin\book1.xls"
"Driver={Microsoft Text Driver (*.txt; *.csv)};DBQ=c:\bin"
To know if your DSN is valid, you better test the DNS outside Laravel.
If you have access to a Linux, you can test it by doing:
apt-get install unixodbc
isql -v DSN_NAME db_username db_password
And it should answer with:
+---------------------------------------+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+---------------------------------------+
SQL>
There's a bug in ccovey's source code and, for now, you should alter the source of ODBCDriverConnection to:
/**
* Default grammar for specified Schema
* #return Schema\Grammars\Grammar
*/
protected function getDefaultSchemaGrammar()
{
return $this->withTablePrefix(new \Illuminate\Database\Schema\SqlServerGrammar);
}
I'll open an issue in the package Github so they get this fixed.

Related

Connect to PostgreSQL DB with 'public' schema

How do I connect to the PostgreSQL database with the following connection info?
I'm using Jupyter Notebook.
from sqlalchemy import create_engine
POSTGRES_DIALECT = 'postgresql'
POSTGRES_SERVER = 'server'
POSTGRES_DBNAME = 'db'
POSTGRES_SCHEMA = 'public'
POSTGRES_USERNAME = 'user'
POSTGRES_PASSWORD = 'password'
postgres_str = ('{dialect}://{username}:{password}#{server}:{schema}/{dbname}'.format(
dialect=POSTGRES_PROVIDER,
server=POSTGRES_SERVER,
dbname=POSTGRES_DBNAME,
schema=POSTGRES_SCHEMA,
username=POSTGRES_USERNAME,
password=POSTGRES_PASSWORD
))
# Create the connection
cnx = create_engine(postgres_str)
ValueError: invalid literal for int() with base 10: 'public'
You are subbing in "schema" where the port belongs. 'public' is not a valid port number.

SQL IM002 Errors while connecting to SQL Server from perl on heroku

I have a perl application hosted on heroku which needs to connect with some sql server. I am unable to establish connection. It fails with following error:
DBI connect('Driver={ODBC Driver 11 For SQL Server}; Server=****; UID=****; PWD=****','',...) failed: [unixODBC][Driver Manager]Data source name not found, and no default driver specified (SQL-IM002) at work.pl line 12.
This is the code.
work.pl
use strict;
use warnings;
use DBI;
my $DRIVER = '{ODBC Driver 11 for SQL Server}';
my $SERVER = '****';
my $UID = '****';
my $PWD = '****';
my $x = DBI->connect("dbi:ODBC:Driver=$DRIVER; Server=$SERVER; UID=$UID; PWD=$PWD");
Relevant Environment Vars:
LIBRARY_PATH=/app/.platform/vendor/usr/lib64:
LD_LIBRARY_PATH=/app/.platform/vendor/usr/lib64:
PATH=/app/.platform/vendor/usr/bin:/app/vendor/perl/bin:/usr/bin:/bin
LANG=en_US.UTF-8
ODBCSYSINI=/app/.platform/vendor/etc
HOME=/app
PWD=/app
ODBCINI=/app/.platform/vendor/etc/odbc.ini
ODBCHOME=/app/.platform/vendor/etc/
PERL5OPT=-Mlocal::lib=/app/vendor/perl-deps
odbcinst.ini:
Description = ODBC for PostgreSQL
Driver = /usr/lib/psqlodbc.so
Setup = /usr/lib/libodbcpsqlS.so
Driver64 = /usr/lib64/psqlodbc.so
Setup64 = /usr/lib64/libodbcpsqlS.so
FileUsage = 1
[MySQL]
Description = ODBC for MySQL
Driver = /usr/lib/libmyodbc5.so
Setup = /usr/lib/libodbcmyS.so
Driver64 = /usr/lib64/libmyodbc5.so
Setup64 = /usr/lib64/libodbcmyS.so
FileUsage = 1
[ODBC Driver 11 for SQL Server]
Description = Microsoft ODBC Driver 11 for SQL Server
Driver = .platform/vendor/opt/microsoft/msodbcsql/lib64/libmsodbcsql-11.0.so.2270.0
Threading = 1
UsageCount = 1
odbc.ini is empty.
Am I missing something?
Tried everything and found the reason. It should be dbd:ODBC:DRIVER instead of dbd:ODBC:Driver.

Enable SQL Server Authentication - DSN Configuration

I'm using Powershell to update DSN configuration. Example code:
$properties = #('server=databaseServer1', 'description=Test description')
Set-OdbcDsn -Name 'TestDSN' -SetPropertyValue $properties -DsnType System
This works. I want to enable SQL Server Authentication, following this page I tried:
$properties = #('SqlPassword=SQL_AU_PASSWORD')
Does not work. Also tried:
$properties = #('Authentication=SqlPassword')
Does not work. Both show the error Invalid keyword-value pairs.
What am I doing wrong?
you have to set Trusted_Connection (No = SQL Server Authentication, Yes = Windows Authentication)
$properties = #('server=databaseServer1', 'description=Test description','Trusted_Connection=No')
Set-OdbcDsn -Name 'TestDSN' -SetPropertyValue $properties -DsnType System

Not able to connect to a remote SQL Server instance

I am trying to find a solution to my problem online but no luck yet. Hence posting the question:
I am running a powershell script which will fetch backup details from all SQL Servers mentioned in a notepad. The script is working fine for SQL Servers till 2008R2. But I am getting error while connecting to SQL Server 2012/2014. The script is running on Windows Server 2008 having SQL Server 2008R2 installed on it. So I will request you to help me to troubleshoot it.
Input File:E:\Sachin\SQL_Servers2.txt
SERVERNAME,SERVERNAME,1433
$servers = Get-Content 'E:\Sachin\SQL_Servers2.txt'
#Create a new Excel object using COM
$Excel = New-Object -ComObject Excel.Application
$Excel.visible = $True
$Excel = $Excel.Workbooks.Add()
$Sheet = $Excel.Worksheets.Item(1)
#Counter variable for rows
$global:intRow = 1
foreach ($sv in $servers) {
# Separate the server and instance names
$srvr = $sv.Split(",")
$server = $srvr[0]
$instance = $srvr[1]
$port = $srvr[2]
getsqlinfo $server $instance $port
function getsqlinfo {
param (
[string]$svr,
[string]$inst,
[string]$port
)
foreach ($instancename in $inst)
{
#Create column headers
$Sheet.Cells.Item($intRow,1) = "INSTANCE NAME:"
$Sheet.Cells.Item($intRow,2) = $instancename
$Sheet.Cells.Item($intRow,1).Font.Bold = $True
$Sheet.Cells.Item($intRow,2).Font.Bold = $True
$global:intRow++
$Sheet.Cells.Item($intRow,1) = "DATABASE NAME"
$Sheet.Cells.Item($intRow,2) = "LAST FULL BACKUP"
$Sheet.Cells.Item($intRow,3) = "LAST DIFFERENTIAL BACKUP"
$Sheet.Cells.Item($intRow,4) = "FULL BACKUP AGE(DAYS)"
#Format the column headers
for ($col = 1; $col -le 4; $col++)
{
$Sheet.Cells.Item($intRow,$col).Font.Bold = $True
$Sheet.Cells.Item($intRow,$col).Interior.ColorIndex = 48
$Sheet.Cells.Item($intRow,$col).Font.ColorIndex = 34
}
$global:intRow++
#######################################################
#This script gets SQL Server database information using PowerShell
$ConnectionString = "data source = $inst,$port; initial catalog = master; trusted_connection = true;"
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
# Create an SMO connection to the instance
$s = New-Object ('Microsoft.SqlServer.Management.Smo.Server')
$s.ConnectionContext.ConnectionString = $ConnectionString
$dbs = $s.Databases
#Formatting using Excel
ForEach ($db in $dbs)
Error Msg:
The following exception was thrown when trying to enumerate the collection: "Failed to connect to server .".
At E:\sachin\BackupDetailsVersion7.ps1:51 char:8
+ ForEach <<<< ($db in $dbs)
+ CategoryInfo : NotSpecified: (:) [], ExtendedTypeSystemException
+ FullyQualifiedErrorId : ExceptionInGetEnumerator
Pre-Checks Done:
I am able to telnet the target SQL Server from the Server where I am running the script.
I am having access to the target SQL Server as well.
Microsoft® Windows PowerShell Extensions for Microsoft® SQL Server® 2012 SP2,Microsoft® SQL Server® 2012 SP2 Shared Management Objects and Microsoft® System CLR Types for Microsoft® SQL Server® 2012 SP2 are already installed on the target Server.
Execution policy is set to unrestricted on the target Server.
I am able to get the SQL Server details on the target machine when checking locally, but not able to get details from other machine.
I am suspecting whether is there any restriction/limitation to connect between different SQL Server versions through powershell (any kind of compatibility issue).
Any help/suggestions will be highly appreciated. Thanks in advance. :)

Why am I getting a segmentation fault with the SQL Server ActiveRecord adapter?

isql runs just fine:
kobey:/etc# isql -v censored censored censored
+---------------------------------------+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+---------------------------------------+
SQL> select count(*) from Members;
+------------+
| |
+------------+
| 1531 |
+------------+
SQLRowCount returns 1
1 rows fetched
SQL> quit
I have tested it with the Ruby ODBC driver:
irb(main):002:0> ODBC.connect('ANAME', 'censored', 'censored')
=> #<ODBC::Database:0x7fc95d428c80>
Here is my connection string for ActiveRecord:
Legacy::LegacyRecord.establish_connection({ :dsn => 'ANAME', :password => 'censored', :username => 'censored', :mode => 'odbc', :adapter => 'sqlserver', :encoding => 'utf8'})
Here is the error I receive:
>> Legacy::Member.first
/usr/lib/ruby/gems/1.8/gems/activerecord-sqlserver-adapter-2.3.8/lib/active_record/connection_adapters/sqlserver_adapter.rb:970: [BUG] Segmentation fault
ruby 1.8.7 (2008-08-11 patchlevel 72) [x86_64-linux]
Aborted
Here are the versions of everything involved:
Debian 5.0.4
activerecord-sqlserver-adapter (2.3.8)
freetds-common 0.82-4
libct4 0.82-4
libsybdb5 0.82-4
freetds-dev 0.82-4
libiodbc2 3.52.6-2
libodbc-ruby1.8 0.9995-1
libdbi-ruby1.8 0.2.2-1
libdbd-odbc-ruby1.8 0.2.2-1
libdbd-odbc-ruby 0.2.2-1
odbcinst1debian1 2.2.11-16
unixodbc 2.2.11-16
libodbcinstq1c2 2.2.11-16
tdsodbc 0.82-4
unixodbc-dev 2.2.11-16
Configs:
odbcinst.ini:
[ODBC Drivers]
FreeTDS = Installed
[FreeTDS]
Description = TDS driver (Sybase/MS SQL)
Driver = /usr/lib/odbc/libtdsodbc.so
Setup = /usr/lib/odbc/libtdsS.so
CPTimeout =
CPReuse =
FileUsage = 1
odbc.ini:
[ODBC]
Trace = No
[ODBC Data Sources]
LATA = FreeTDS
[LATA]
Driver = /usr/lib/odbc/libtdsodbc.so
Description = MS SQL Server connection to ANAME legacy database
ServerName = ANAME
Database = aname
freetds.conf:
[global]
[ANAME]
host = 10.0.0.5
tds version = 7.0
client charset = UTF-8
text size = 262144
port = 1433
Does anybody have any idea what might cause this?
After experiencing precisely the same problem, I've found a solution. Use the TinyTDS library instead and avoid the crash in the ODBC library.
This reference on using ActiveRecord with TinyTDS explains more.
First you no longer need dbd/dbi with the 2.3.8. Adapter.
Second:do you have a ntext or nvarchar(max) Field in your Table?

Resources