QSqlDatabase [ODBC] 的远程连接问题
Remote connectivity issue with QSqlDatabase [ODBC]
我正在尝试连接到远程 SQL
服务器:
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC3");
db.setDatabaseName(QString("Driver={SQL Server Native Client 10.0};Server=X.X.X.X;Database=/*DB*/;"));
db.setUserName("sa");
db.setPassword("/*password*/");
if (!db.open()) {
qDebug() << db.lastError().text();
}
else
{
qDebug() << "connected";
}
但大约 7 秒后,它给了我这个日志:
[Microsoft][SQL Server Native Client 10.0]Named Pipes Provider: Could
not open a connection to SQL Server [1326]. [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. [Microsoft][SQL Server Native Client
10.0]Login timeout expired QODBC3: Unable to connect
但是服务器已启动并且 运行 并且连接正常并且已经使用 UDL
文件进行了测试。
注意:本地连接正常
使用这些设置,有用户并传入数据库名,请:
QString connectString = "Driver={SQL Server Native Client 10.0};"; // Driver can also be {SQL Server Native Client 11.0}
connectString.append("Server=SERVERHOSTNAME\SQLINSTANCENAME;"); // Hostname,SQL-Server Instance
connectString.append("Database=SQLDBSCHEMA;"); // Schema
connectString.append("Uid=SQLUSER;"); // User
connectString.append("Pwd=SQLPASS;"); // Pass
db.setDatabaseName(connectString);
if(db.open())
{
ui->statusBar->showMessage("Connected");
}else{
ui->statusBar->showMessage("Not Connected");
}
或者没有使用完整的 DSN
QString connectString = "Driver={SQL Server};"; // Driver is now {SQL Server}
connectString.append("Server=10.1.1.15,5171;"); // IP,Port
connectString.append("Database=SQLDBSCHEMA;"); // Schema
connectString.append("Uid=SQLUSER;"); // User
connectString.append("Pwd=SQLPASS;"); // Pass
db.setDatabaseName(connectString);
if(db.open())
{
ui->statusBar->showMessage("Connected");
}else{
ui->statusBar->showMessage("Not Connected");
}
要获得基于 ip/port 的连接,重要的是在服务器配置中启用此类连接。默认一个不能直接使用ip/port连接。
我正在尝试连接到远程 SQL
服务器:
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC3");
db.setDatabaseName(QString("Driver={SQL Server Native Client 10.0};Server=X.X.X.X;Database=/*DB*/;"));
db.setUserName("sa");
db.setPassword("/*password*/");
if (!db.open()) {
qDebug() << db.lastError().text();
}
else
{
qDebug() << "connected";
}
但大约 7 秒后,它给了我这个日志:
[Microsoft][SQL Server Native Client 10.0]Named Pipes Provider: Could not open a connection to SQL Server [1326]. [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. [Microsoft][SQL Server Native Client 10.0]Login timeout expired QODBC3: Unable to connect
但是服务器已启动并且 运行 并且连接正常并且已经使用 UDL
文件进行了测试。
注意:本地连接正常
使用这些设置,有用户并传入数据库名,请:
QString connectString = "Driver={SQL Server Native Client 10.0};"; // Driver can also be {SQL Server Native Client 11.0}
connectString.append("Server=SERVERHOSTNAME\SQLINSTANCENAME;"); // Hostname,SQL-Server Instance
connectString.append("Database=SQLDBSCHEMA;"); // Schema
connectString.append("Uid=SQLUSER;"); // User
connectString.append("Pwd=SQLPASS;"); // Pass
db.setDatabaseName(connectString);
if(db.open())
{
ui->statusBar->showMessage("Connected");
}else{
ui->statusBar->showMessage("Not Connected");
}
或者没有使用完整的 DSN
QString connectString = "Driver={SQL Server};"; // Driver is now {SQL Server}
connectString.append("Server=10.1.1.15,5171;"); // IP,Port
connectString.append("Database=SQLDBSCHEMA;"); // Schema
connectString.append("Uid=SQLUSER;"); // User
connectString.append("Pwd=SQLPASS;"); // Pass
db.setDatabaseName(connectString);
if(db.open())
{
ui->statusBar->showMessage("Connected");
}else{
ui->statusBar->showMessage("Not Connected");
}
要获得基于 ip/port 的连接,重要的是在服务器配置中启用此类连接。默认一个不能直接使用ip/port连接。