简体   繁体   English

使用SOCI从Microsoft SQL Server获取bigint

[英]Get bigint from Microsoft SQL Server with SOCI

I am trying to get a bigint value (named "NUM" in my example) that comes from my database (Microsoft SQL Server). 我试图获取来自数据库(Microsoft SQL Server)的bigint值(在本示例中为“ NUM”)。 In the following code, I am trying to get it as an int even if I know that an int is smaller than a bigint but the cast always fails. 在以下代码中,即使我知道一个int小于bigint但我的转换总是失败,我仍试图将它作为int进行获取。 I also tried to get it as a long long int and as some other types of variable but I am always getting the same problem at runtime as shown below. 我还尝试将它作为long long int以及其他一些类型的变量来获取,但是在运行时我总是遇到相同的问题,如下所示。

try
{
    session sql(odbc, "...");
    rowset<row> rs = (sql.prepare << "SELECT MAX(NUM) AS MAX_NUM FROM T1");

    for (rowset<row>::const_iterator it = rs.begin(); it != rs.end(); ++it)
    {
        row const& row = *it;

        cout << "MAX_NUM: " << row.get<int>("MAX_NUM") << endl;
    }

}
catch (exception const &e)
{
    cerr << "Error: " << e.what() << '\n';
}

I have also tried the following getters: 我还尝试了以下吸气剂:

cout << "MAX_NUM: " << row.get<long>("MAX_NUM") << endl;
cout << "MAX_NUM: " << row.get<long long>("MAX_NUM") << endl;
cout << "MAX_NUM: " << row.get<long long int>("MAX_NUM") << endl;

All my tests print the same error when I run my application: 运行我的应用程序时,我所有的测试都输出相同的错误:

Error: std::bad_cast

My question is : What is the correct way to get this value ? 我的问题是:获得此价值的正确方法是什么?

我不知道这是否是最好的解决方案,但这是我发现的唯一可行的解​​决方案:

cout << "MAX_NUM: " << row.get<double>("MAX_NUM") << endl;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM