开发者

How to access SQL DB in C++?

开发者 https://www.devze.com 2023-02-20 08:54 出处:网络
I am looking for a free cross-platform (Windows, Linux) C++ library for an access to an MS SQL server DB.

I am looking for a free cross-platform (Windows, Linux) C++ library for an access to an MS SQL server DB.

For example, I want to convert the following C# flow to unmanaged C or C++ (from here) :

static void Main()
{
    string connectionString = 
       ConsoleApplication746.Properties.Settings.Default.ConnectionString;
    using (SqlConnection con = new SqlConnection(connectionString))
    {
        con.Open();
        using (SqlCommand开发者_JAVA百科 command = 
           new SqlCommand("SELECT TOP 2 * FROM Dogs1", con))
        using (SqlDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                 Console.WriteLine("{0} {1} {2}", reader.GetInt32(0),
                    reader.GetString(1), reader.GetString(2));
            }
        }
    }
}


Try DTL or SOCI.

Edit: or OTL.


Accessing MS SQL server from windows should not be a problem in general. For accessing it from Linux, this web page may help you to find a way

http://www.sommarskog.se/mssqlperl/unix.html

Finding a solution that works on both Windows and Linux is a task that may not be solved easily, depends on the exact requirements and your current architecture. For example, when you just need C++ program calling a Perl script which does the DB work for you, this path may provide you with a free cross-platform solution. Perl is free and cross-platform, and the DBI modules too.

On the other hand, when your cross-platform C++ program uses a cross-platform framework like Qt, and you need a direct connection in your C++ program to the SQL server, ODBC may be the best option:

http://doc.qt.nokia.com/4.7/sql-driver.html

Unfortunately, according to the information behind my first link, there are no free ODBC drivers for Linux, so you might bite the bullet and buy one.


Indeed, SQLite is an implementation of a DB where data is stored in a flat file.

I think ODBC might be the technology you're looking for.


What about SQLAPI++? Works both on Windows and Linux/Unix. http://www.sqlapi.com/.


There is no reason to rewrite perfectly good, tested, working code.

Don't do it. Use Mono to run your existing code on Unix/Linux instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消