Код: Выделить всё
#include
#include
#include
#include
#include
int main() {
SQLHENV hEnv;
SQLHDBC hDbc;
SQLRETURN ret; /* ODBC API return status */
SQLCHAR* connStr = (SQLCHAR*)"DRIVER={ODBC Driver 17 for SQL Server}; SERVER={tcp:xxx.xxx.xxx.xxx,xxxx}; UID=****; PWD=*****; DATABASE={SDC};\0";
printf("%s\n", connStr);
// Allocate an environment handle
ret = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnv);
if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO) {
printf("Error allocating environment handle\n");
return -1;
}
// Set the ODBC version environment attribute
ret = SQLSetEnvAttr(hEnv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0);
if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO) {
printf("Error setting environment attribute\n");
SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
return -1;
}
// Allocate a connection handle
ret = SQLAllocHandle(SQL_HANDLE_DBC, hEnv, &hDbc);
if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO) {
printf("Error allocating connection handle\n");
SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
return -1;
}
// Set login timeout to 5 seconds
SQLSetConnectAttr(hDbc, SQL_LOGIN_TIMEOUT, (SQLPOINTER)5, 0);
// Connect to the SQL Server
ret = SQLDriverConnect(hDbc, NULL, connStr, SQL_NTS, NULL, 0, NULL, SQL_DRIVER_COMPLETE);
if (ret == SQL_ERROR) {
SQLCHAR sqlState[12] = { 0 }; // 5 characters + null terminator
SQLINTEGER nativeError;
SQLCHAR messageText[2048] = { 0 }; // Message buffer
SQLSMALLINT messageLength;
// Fetch diagnostic information
ret = SQLGetDiagRec(SQL_HANDLE_DBC, hDbc, 1, sqlState, &nativeError, messageText, sizeof(messageText), &messageLength);
// Null-terminate the message if it exceeds the buffer size
if (messageLength > sizeof(messageText) - 1) {
messageText[sizeof(messageText) - 1] = '\0';
}
printf("SQLSTATE: %s\n", sqlState);
printf("Native Error Code: %d\n", nativeError);
printf("Message: %s\n", messageText);
}
else if (ret == SQL_SUCCESS_WITH_INFO) {
printf("Connection successful with info!\n");
}
else if (ret == SQL_SUCCESS) {
printf("Connection successful!\n");
}
else {
printf("Error connecting to the database\n");
SQLFreeHandle(SQL_HANDLE_DBC, hDbc);
SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
return -1;
}
// Close the connection and free handles
SQLDisconnect(hDbc);
SQLFreeHandle(SQL_HANDLE_DBC, hDbc);
SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
return 0;
}
Код: Выделить всё
import pyodbc as sql
import pandas as pd
import sys
import warnings
from time import (strftime, strptime)
server = 'tcp:xxx.xxx.xxx.xxx,xxxx'
db = 'SDC'
string = "DRIVER={ODBC Driver 17 for SQL Server}; SERVER={"+server+"}; UID=****; PWD=*****; DATABASE={"+db+"}; "
print(string)
try:
db = sql.connect(string)
print('It worked!')
except Exception as ex:
print(ex)
Подробнее здесь: https://stackoverflow.com/questions/789 ... connection