Learning to connect R to SQL database - sql

I am just learning how to use R to connect to SQL database. When I run my code, I am getting a message box says:
library(odbc)
con <- dbConnect(odbc(),Driver = "ODBC Driver 17 for SQL Serve",
Server ="DESKTOP-SHXXX",
Database ="BikeStores",
UID="robin",
PWD = rstudioapi::askForPassword("xxx"),
Trusted_Connection ="yes")
# Error: nanodbc/nanodbc.cpp:1021: 08001: [Microsoft][ODBC Driver 17 for SQL
# Server]Named Pipes Provider: Could not open a connection to SQL Server [2].
# [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired
# [Microsoft][ODBC Driver 17 for SQL Server]A network-related or instance-
# specific error has occurred while establishing a connection to SQL Server.
# Server is not found or not accessible. Check if instance name is correct
# and if SQL Server is configured to allow remote connections. For more
# information see SQL Server Books Online.
Any help?

Related

bcp to extract data from SQL query to local machine

I am using below code
bcp "SELECT COL1 FROM DBO.V_VIEW_1" queryout d:\VIEW_PART1.csv -S AB6Y7YYY,1433 -c -t",", -T
AB6Y7YYY : I got this with below code in SSMS
SELECT ##SERVERNAME
I am able to see VIEW_PART1.csv in d drive but it is empty and error in command line is as below
SQLState = 08001, NativeError = 10061
Error = [Microsoft][ODBC Driver 17 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.
SQLState = S1T00, NativeError = 0
Error = [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired
What am i doing wrong?

PowerShell: network-related or instance-specific error occurred while establishing a connection to SQL Server

On my Skype For Business Server, when I run the following command, my Queue get's changed as it should:
Set-CsRgsQueue -Instance $queue
On my linked server however (one hop away from the Lync server), i get the following error:
network-related or instance-specific error occurred while
establishing a connection to SQL Server. Set-CsRgsQueue : A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the
instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
I am however able to set up ODBC to the remote server (+ opened FW 1433 port, restarted the service ,....)
Where could my problem be?
EDIT:
My scriptblock looks like:
import-module Lync
$queue = Get-CsRgsQueue -Name $toChange
$action = New-CsRgsCallAction -Action TransferToPSTN -Uri $forwarduri -QueueID $queue.Identity
$queue.OverflowAction = $action
$queue.OverflowThreshold = 0
Set-CsRgsQueue -Instance $queue
If i'm not able to solve the issue, how could I implement invoke-command as this works on the server itself?
I tried by doing:
Invoke-Command -ComputerName SQLSERVER -ScriptBlock {Set-CsRgsQueue -Instance $queue}
but this throws:
Cannot validate argument on parameter 'Instance'. The argument is null. Provide a valid value for the argument, and then try running the command again.

Airflow Adaptive Server connection failed

I want to connect my Airflow and Microsoft SQL Server. I configured my connection under 'connections' bar in 'Admin' box as mentioned in the following link:
http://airflow.apache.org/howto/manage-connections.html
But when I run my Dag task that is related to SQL server immedatly fails by following error:
[2019-03-28 16:16:07,439] {models.py:1788} ERROR - (18456, "Login failed for user 'XXXX'.DB-Lib error message 20018, severity 14:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (***.***.***.28:1433)\n")
My code from DAG for Micrososft Sql Connection is following:
sql_command = """
select * from [sys].[tables]
"""
t3 = MsSqlOperator( task_id = 'run_test_proc',
mssql_conn_id = 'FIConnection',
sql = sql_command,
dag = dag)
I verified ip address and port number kind of configuration things by establishing connection through pymssql library from my local computer. Test code is following:
pymssql.connect(server="***.***.***.28:1433",
user="XXXX",
password="XXXXXX"
) as conn:
df = pd.read_sql("SELECT * FROM [sys].[tables]", conn)
print(df)
Could you please share if you have experienced this issue?
By the way I am using VirtualBox in Ubuntu 16.04 LTS
I had the same problem because freetds-dev was missing on linux:
apt-get install freetds-dev

Creating bcp format file from local database table - connection error

I need to create a format file from a local database table but I get a connection error when I try to run the command in cmd. Here is the command I am using:
bcp OrderDatabase.dbo.Orders format nul -c -f D:\Format\Orders.fmt -T
I get the following errors:
SQLState = 08001, NativeError = 2
Error = [Microsoft][SQL Server Native Client 11.0]Named Pipes Provider: Could not open a connection to SQL Server [2].
SQLState = 08001, NativeError = 2
Error = [Microsoft][SQL Server Native Client 11.0]A network-related or instance-
specific error has occurred while establishing a connection to SQL Server.
Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.
SQLState = S1T00, NativeError = 0
Error = [Microsoft][SQL Server Native Client 11.0]Login timeout expired
I have checked Services.msc and SQL Server is indeed "Started". I have also checked Sql Server Configuration Manager and the SQL Server state is "Running" and Start Mode is "Automatic".
The connection string to the local database is:
Data Source=(LocalDB)\v11.0;AttachDbFilename=D:\Tools_TextileMagazine\CSharp_Template\Tools_TextileMagazine\OrderDatabase.mdf;Integrated Security=True
Any ideas to what could be wrong?
Add the -S parameter like specified here:
http://msdn.microsoft.com/it-it/library/ms162802.aspx
So your connection string will be:
bcp OrderDatabase.dbo.Orders format nul -c -f D:\Format\Orders.fmt -T -S localhost\V11.0
I assume from your connection string that you named istance: V11.0

Getting Error with an SQL query

I'm getting an error when running this query in SQL Server 2008:
EXEC master..xp_cmdshell'bcp
"select Row_ID, Service_Code,Skill from Account_Service"
queryout "d:\text.txt" -c -T -x'
Error is:
SQLState = 08001, NativeError = 2
Error = [Microsoft][SQL Server Native Client 10.0]Named Pipes Provider: Could not open a connection to SQL Server [2].
SQLState = 08001, NativeError = 2
Error = [Microsoft][SQL Server Native Client 10.0]A network-related or instance-specific error has occurred while
establishing a connection to SQL Server. Server is not found or not
accessible. Check if instance name is correct and if SQL Server is
configured to allow remote connections. For more information see
SQL Server Books Online.
SQLState = S1T00, NativeError = 0
Error = [Microsoft][SQL Server Native Client 10.0]
Login timeout expired
NOTE: Shared Memory, Named Pipes, TCP/IP are all enabled in SQL Server Configuration Manager
Look into the SQL Configuration Manager.
I guess TCP/IP Protocoll is not enabled, so you can Access your SQL Server via. Shared Memory.
So you would be able to Connect if you are on the SQL Server it self but can not Access it from another Maschine.
Otherwise I would say it is an Network Problem as you see the Connection times out. Maybe a Firewall Software is blocking or your Server can not be reached.
The most likely Cause I expririenced, fitting to oyur discrition is disabled TCP/IP Protocol for SQL Server.