반응형


https://technet.microsoft.com/ko-kr/library/hh568451(v=sql.110).aspx


Linux의 Microsoft ODBC Driver for SQL Server


- ODBC driver for SQL Server를 사용하면 Linux에서 실행 중인 기본 응용 프로그램(C/C++)을 SQL Server 2008, SQL Server 2008 R2, SQL Server 2012 및 Microsoft Azure SQL 데이터베이스에 연결할 수 있습니다.

===============================================================

ODBC Driver 다운로드( ODBC Driver 13.0 for Linux Released )

https://blogs.msdn.microsoft.com/sqlnativeclient/2016/10/20/odbc-driver-13-0-for-linux-released/



Hi all, we are delighted to share the Production Ready release of the Microsoft ODBC Driver 13 for Linux –  (Ubuntu, RedHat and SUSE).
The new driver enables access to SQL Server, Azure SQL Database and Azure SQL DW from any C/C++ application on Linux

What’s new
•Native Linux Install Experience: The driver can now be installed with apt-get (Ubuntu), yum (RedHat/CentOS) and Zypper (SUSE). Instructions on how to do this is below.
•AlwaysOn Availability Groups (AG): The driver now supports transparent connections to AlwaysOn Availability Groups. The driver quickly discovers the current AlwaysOn topology of your server infrastructure and connects to the current active server transparently.
•TLS 1.2 support: The driver now supports TLS 1.2 connections to SQL Server.

Install the ODBC Driver for Linux on Ubuntu 15.04

1.sudo su
2.sh -c 'echo "deb [arch=amd64] https://apt-mo.trafficmanager.net/repos/mssql-ubuntu-vivid-release/ vivid main" > /etc/apt/sources.list.d/mssqlpreview.list'
3.sudo apt-key adv --keyserver apt-mo.trafficmanager.net --recv-keys 417A0893
4.apt-get update
5.apt-get install msodbcsql
6.apt-get install unixodbc-dev-utf16 #this step is optional but recommended*


Install the ODBC Driver for Linux on Ubuntu 15.10

1.sudo su
2.sh -c 'echo "deb [arch=amd64] https://apt-mo.trafficmanager.net/repos/mssql-ubuntu-wily-release/ wily main" > /etc/apt/sources.list.d/mssqlpreview.list'
3.sudo apt-key adv --keyserver apt-mo.trafficmanager.net --recv-keys 417A0893
4.apt-get update
5.apt-get install msodbcsql
6.apt-get install unixodbc-dev-utf16 #this step is optional but recommended*


Install the ODBC Driver for Linux on Ubuntu 16.04

1.sudo su
2.sh -c 'echo "deb [arch=amd64] https://apt-mo.trafficmanager.net/repos/mssql-ubuntu-xenial-release/ xenial main" > /etc/apt/sources.list.d/mssqlpreview.list'
3.sudo apt-key adv --keyserver apt-mo.trafficmanager.net --recv-keys 417A0893
4.apt-get update
5.apt-get install msodbcsql
6.apt-get install unixodbc-dev-utf16 #this step is optional but recommended*


Install the ODBC Driver for Linux on RedHat 6

1.sudo su
2.yum-config-manager --add-repo https://apt-mo.trafficmanager.net/yumrepos/mssql-rhel6-release/
3.yum-config-manager --enable mssql-rhel6-release
4.wget "http://aka.ms/msodbcrhelpublickey/dpgswdist.v1.asc"
5.rpm --import dpgswdist.v1.asc
6.yum remove unixODBC #to avoid conflicts during installation
7.yum update
8.yum install msodbcsql
9.yum install unixODBC-utf16-devel #this step is optional but recommended*


Install the ODBC Driver for Linux on RedHat 7

1.sudo su
2.yum-config-manager --add-repo https://apt-mo.trafficmanager.net/yumrepos/mssql-rhel7-release/
3.yum-config-manager --enable mssql-rhel7-release
4.wget "http://aka.ms/msodbcrhelpublickey/dpgswdist.v1.asc"
5.rpm --import dpgswdist.v1.asc
6.yum remove unixODBC #to avoid conflicts during installation
7.yum update
8.yum install msodbcsql
9.yum install unixODBC-utf16-devel #this step is optional but recommended*


Install the ODBC Driver for SUSE12

1.zypper ar https://apt-mo.trafficmanager.net/yumrepos/mssql-suse12-release/ "mssql" #To add the repo
2.wget "http://aka.ms/msodbcrhelpublickey/dpgswdist.v1.asc"
3.rpm --import dpgswdist.v1.asc
4.zypper remove unixODBC #to avoid conflicts
5.zypper update
6.zypper install msodbcsql
7.zypper install unixODBC-utf16-devel #this step is optional but recommended*


Note: Packages for SQL Server command line tools will be available soon. The above mentioned packages only install the ODBC Driver for SQL Server that enable connectivity from any C/C++ application.

Try our Sample

Once you install the driver that runs on a supported Linux distro, you can use this C sample to connect to SQL Server/Azure SQL DB/Azure SQL DW. To download the sample and get started, follow these steps:

1.wget "https://raw.githubusercontent.com/Microsoft/sql-server-samples/master/samples/connect/c/linux/sample_c_linux.c"
2.gcc sample_c_linux.c -o sample_c_linux -lodbc -w #make sure you change the servername, username and password in the connections string
3../sample_c_linux


If you installed the driver using the manual instructions found here, you will have to manually uninstall the ODBC Driver and the unixODBC Driver Manager to use the deb/rpm packages. If you have any questions on how to manually uninstall, feel free to leave a comment below.

Please fill bugs/questions/issues on our Issues page. We welcome contributions/questions/issues of any kind. Happy programming!

Survey and Future Plans

Please take this survey to help prioritize features and scenarios for the next release of the ODBC Driver for Linux. Going forward we plan to expand SQL Server 16 Feature Support (example: Always Encrypted), improve test coverage, and fix bugs reported on our Issues page.

Please stay tuned for upcoming releases that will have additional feature support and bug fixes. This applies to our wide range of client drivers including PHP 7.0, Node.js, JDBC and ADO.NET which are already available.

Meet Bhagdev (meetb@microsoft.com)


===============================================================


<샘플>

https://raw.githubusercontent.com/Microsoft/sql-server-samples/master/samples/connect/c/linux/sample_c_linux.c


===============================================================

#include <stdio.h>
#include <stdlib.h>
#include <sql.h>
#include <sqlext.h>
#include <string.h>

#define RESULT_LEN  256

#define CHECK_ERROR(e, s, h, t) ({\
            if (e!=SQL_SUCCESS && e != SQL_SUCCESS_WITH_INFO) {extract_error(s, h, t); goto exit;} \
})
void extract_error(char *fn, SQLHANDLE handle, SQLSMALLINT type)
{
    SQLINTEGER i = 0;
    SQLINTEGER NativeError;
    SQLCHAR SQLState[ 7 ];
    SQLCHAR MessageText[256];
    SQLSMALLINT TextLength;
    SQLRETURN ret;

    fprintf(stderr, "\nThe driver reported the following error %s\n", fn);
    do
    {
        ret = SQLGetDiagRec(type, handle, ++i, SQLState, &NativeError,
                            MessageText, sizeof(MessageText), &TextLength);
        if (SQL_SUCCEEDED(ret)) {
            printf("%s:%ld:%ld:%s\n",
                        SQLState, (long) i, (long) NativeError, MessageText);
        }
    }
    while( ret == SQL_SUCCESS );
}
int main () {

    SQLHENV   henv  = SQL_NULL_HENV;   // Environment
    SQLHDBC   hdbc  = SQL_NULL_HDBC;   // Connection handle
    SQLHSTMT  hstmt = SQL_NULL_HSTMT;  // Statement handle
    SQLRETURN retcode;
    SQLCHAR strResult[RESULT_LEN];
    SQLCHAR outstr[1024];
    SQLSMALLINT outstrlen;
    int i=0;
    // Allocate environment handle
    retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
    CHECK_ERROR(retcode, "SQLAllocHandle(SQL_HANDLE_ENV)",
                henv, SQL_HANDLE_ENV);
    // Set the ODBC version environment attribute
    retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION,
                            (SQLPOINTER*)SQL_OV_ODBC3, 0);
    CHECK_ERROR(retcode, "SQLSetEnvAttr(SQL_ATTR_ODBC_VERSION)",
                henv, SQL_HANDLE_ENV);
    // Allocate connection handle
    retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
    CHECK_ERROR(retcode, "SQLAllocHandle(SQL_HANDLE_DBC)",
                hdbc, SQL_HANDLE_DBC);
    // Set login timeout to 5 seconds
    SQLSetConnectAttr(hdbc, SQL_LOGIN_TIMEOUT, (SQLPOINTER)5, 0);
    CHECK_ERROR(retcode, "SQLSetConnectAttr(SQL_LOGIN_TIMEOUT)",
                hdbc, SQL_HANDLE_DBC);
    // Connect to data source
    retcode = SQLDriverConnect(hdbc, NULL, "Driver=ODBC Driver 13 for SQL Server;Server=<yourserver>;Uid=<yourusername>;Pwd=<yourpassword>;database=<yourdatabase>", SQL_NTS, outstr, sizeof(outstr), &outstrlen, SQL_DRIVER_NOPROMPT);
    // Allocate statement handle
    retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
    CHECK_ERROR(retcode, "SQLAllocHandle(SQL_HANDLE_STMT)",
                hstmt, SQL_HANDLE_STMT);
    retcode = SQLExecDirect(hstmt,
                            (SQLCHAR*) "SELECT @@Version", SQL_NTS);
    CHECK_ERROR(retcode, "SQLExecDirect()", hstmt, SQL_HANDLE_STMT);

    // Bind columns

    retcode = SQLBindCol(hstmt, 1, SQL_C_CHAR, &strResult, RESULT_LEN, 0);


    // Fetch and print each row of data until SQL_NO_DATA returned.
    for (i=0; ; i++) {
        retcode = SQLFetch(hstmt);
        if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
               printf("Result is: %s", strResult);
        } else {
            if (retcode != SQL_NO_DATA) {
                CHECK_ERROR(retcode, "SQLFetch()", hstmt, SQL_HANDLE_STMT);
            } else {
                break;
            }
        }
    }

exit:

    printf ("\nComplete.\n");
    // Free handles
    // Statement
    if (hstmt != SQL_NULL_HSTMT)
        SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
    // Connection
    if (hdbc != SQL_NULL_HDBC) {
        SQLDisconnect(hdbc);
        SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
    }
    // Environment
    if (henv != SQL_NULL_HENV)
        SQLFreeHandle(SQL_HANDLE_ENV, henv);
    return 0;
}


===============================================================

 

Use Existing MSDN C++ ODBC Samples for Microsoft Linux ODBC Driver

https://blogs.msdn.microsoft.com/sqlblog/2012/01/26/use-existing-msdn-c-odbc-samples-for-microsoft-linux-odbc-driver/

반응형
Posted by 공간사랑
,