sql connectionstring一步一步调试?

时间:2011-04-26 19:45:25

标签: c++ sql-server ado

你好,我希望这是我关于sql connectionsstrings的最后一篇文章......

我有一个用VS2010Express编写的win32console应用程序,我尝试连接我的SQL服务器。 我认为我做的一切正确,“唯一”的问题是连接字符串。有人可以确认代码中没有其他大错误吗?

关于连接线,我试过试过.... 一个站点建议你可以通过在桌面上创建一个connect.udl文件来获得一个有效的字符串,双击它并按照你的方式完成对话。在texteditor中再次打开时,会找到正确的连接字符串。 在我的情况下,这将是:

  

Provider = SQLOLEDB.1; Persist Security Info = False; User ID = Test; Initial Catalog = Stackhoover; Data Source = PETERPAN \ SQLEXPRESS

也没有工作....有没有办法“调试”字符串?要查看哪个部分是错误的,例如“错误的pwd”或“找不到服务器”? 防火墙现在关闭,所以这不是问题。 _hr返回“E_Fail” 调试器进入msado15.tli line1271

感谢您的帮助! 快乐的编码!

#include "stdafx.h"
void HandleError(HRESULT)
{
MessageBox( NULL, L"ERROR", L"XX!",MB_ICONEXCLAMATION |MB_RETRYCANCEL);
}

int _tmain(int argc, _TCHAR* argv[])
{
HRESULT hr;                                     //http://en.wikipedia.org/wiki/HRESULT  

//creating a connection object
USEADO::_ConnectionPtr connection;
//create a recordset object
USEADO::_RecordsetPtr recordset;

 //      Initialize COM  
    if(FAILED(hr = CoInitialize(NULL)))
    {
        HandleError(hr); 
        return hr;
    }   

if(FAILED(hr = connection.CreateInstance(__uuidof(USEADO::Connection))))
    {
        HandleError(hr); 
        return hr;
    }

if(FAILED(hr = recordset.CreateInstance(__uuidof(USEADO::Recordset))))
    {
        HandleError(hr); 
        return hr;
    }

    connection->CursorLocation = USEADO::adUseServer; //http://dev.mysql.com/tech-resources/articles/vb-cursors-and-locks.html
                                       //adUseClient;


    //Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
    connection->Open(L"Data Source=PETERPAN\SQLEXPRESS;Initial Catalog=Stackhoover; User Id=Test; Password = Test", L"", L"", USEADO::adConnectUnspecified);
    //connection->Open(L"Provider=SQLOLEDB.1;Persist Security Info=False;User ID=Test;Initial Catalog=Stackhoover;Data Source=PETERPAN\SQLEXPRESS", L"Test", L"Test", USEADO::adConnectUnspecified);
    //DRIVER=SQL Native Client;SERVER=.\SQLExpress;Trusted_Connection=Yes;Initial Catalog=ADOTest;
    //connection->Open(L"DRIVER=SQL Native Client;SERVER=PETERPAN\SQLEXPRESS;Trusted_Connection=Yes;Initial Catalog=Stackhoover;", L"", L"", USEADO::adConnectUnspecified);

return 0;
}

我的stdafx.h

 #pragma once
#import "C:\Program\Delade filer\System\ado\msado15.dll"    rename_namespace("USEADO"),rename("EOF","EndOfFile")
// Define ADO Namespace as global
using namespace USEADO;

#include "targetver.h"
#include <stdio.h>
#include <tchar.h>

2 个答案:

答案 0 :(得分:3)

你需要逃避反斜杠,所以:

connection->Open(L"Data Source=PETERPAN\SQLEXPRESS;Initial Catalog=Stackhoover; User Id=Test; Password = Test", L"", L"", USEADO::adConnectUnspecified);

应该是:

connection->Open(L"Data Source=PETERPAN\\SQLEXPRESS;Initial Catalog=Stackhoover; User Id=Test; Password = Test", L"", L"", USEADO::adConnectUnspecified);

答案 1 :(得分:1)

如果您有User ID,那么您需要password。我也会忽略Persist Security Info。你需要一个尾随;。请参阅ConnectionStrings.comMS page

我希望连接字符串看起来更像是其中之一,具体取决于您使用的是SQL Server登录还是Windows身份验证

  

Provider = SQLOLEDB.1; User ID = Test; Password = foo; Database = Stackhoover; Server = PETERPAN \ SQLEXPRESS;

     

Provider = SQLOLEDB.1; Integrated Security = true; Database = Stackhoover; Server = PETERPAN \ SQLEXPRESS;