2011-12-09 45 views
0

我在Visual Studio 2008中開發了一個簡單的應用程序。它是C和C++的組合。現在我試圖連接到SQLserver2005.安裝了SQlserver2005 managementExpress studio。 創建數據庫和表格。瞭解連接性是以C代碼插入數據爲例,但它沒有顯示任何東西。我知道它非常簡單,但有些地方做錯了。 我想澄清一些關於下面的代碼的東西,想通過使用SQLserver2005來做本地連接而不是服務器部分。SQLserver2005與VC++連接(代碼)

  1. 在連接字符串我應該提及的用戶名和密碼。
  2. 這條線真正做了什麼。「hr = pConn-> Open(strCon,」keerth「,」keerth「,0);」
  3. 通常在MYSQL中我們將創建DNS.but如何提及SQL server2005中的DNS。
  4. 如果我想插入變量值(它將顯示在控制檯窗口中)到database.kindly讓我知道這有可能嗎?如果是的話,建議我實施它的任何想法。

是否有任何教程鏈接來了解這些事情。

#include "stdafx.h" 

#import "C:\Program Files\Common Files\System\ADO\msado15.dll" \ 
no_namespace rename("EOF", "EndOfFile") 

int main(int argc, char* argv[]) 
{ 

    /*The following variables will be initialized with necessary values and appended to the strSQL values*/ 
    _bstr_t strName; 
    _bstr_t strAge; 
    _bstr_t strDOB; 
    _bstr_t strSalary; 

     _ConnectionPtr pConn = NULL; 
    // Define string variables for ADO connection 
    _bstr_t strCon("Provider=SQLOLEDB.1;Persist Security Info=False;Username=keerth;Password=keerth;Initial Catalog=keerth516;Data Source=(local);Integrated Security=SSPI;"); 

    HRESULT hr = S_OK; 

    //Initialize the COM Library 
    CoInitialize(NULL); 

    try 
    { 
     //Create the Connection pointer 
     hr = pConn.CreateInstance((__uuidof(Connection))); 
     if(FAILED(hr)) 
     { 
      printf("Error instantiating Connection object\n"); 
      goto cleanup; 
     } 

     //Open the SQL Server connection 
     hr = pConn->Open(strCon,"keerth","keerth",0); 
     if(FAILED(hr)) 
     { 
       printf("Error Opening Database object using ADO _ConnectionPtr \n"); 
       goto cleanup; 
     } 

     /*Initialize the values */ 
     strName = "'C++ ADO insert Sample',"; 
     strAge = "23,"; 
     strDOB = "'13/04/1988',"; 
     strSalary = "16600.10)"; 

     /* Append the values to the Insert Statement */ 
     _bstr_t strSQL("Insert into Table1(NAME,AGE,DOB,SALARY) Values("); 

     strSQL += strName + strAge + strDOB + strSalary ; 

     printf("%s\n",(LPCSTR)strSQL); 

     //Execute the insert statement 
     pConn->Execute(strSQL,NULL,adExecuteNoRecords); 

     printf("Data Added Successfully\n",(LPCSTR)strSQL); 

     //Close the database 
     pConn->Close(); 

    } 
    catch(_com_error &ce) 
    { 
     //printf("Error:%s\n",ce.ErrorInfo); 
     printf("Error:%s\n,",(char*)ce.Description()); 
    } 


cleanup: 
    CoUninitialize(); 

    return 0; 
} 

回答

1

您可能想看看ADO API http://msdn.microsoft.com/en-us/library/windows/desktop/ms678086(v=vs.85).aspx

我認爲提供用戶名/密碼作爲連接字符串中的參數打開的參數是沒有意義的。設置「Integrated Security = SSPI」會激活集成的Windows登錄,所以這甚至會使您的用戶名/密碼更加冗餘。連接字符串記錄在MSDN(上面的鏈接)中,特定於數據庫提供程序的連接字符串部分記錄在此處:http://msdn.microsoft.com/en-us/library/windows/desktop/ms681020(v=vs.85).aspx

「pConn-> Open(strCon,」keerth「,」keerth「,0);」打開與數據庫服務器的連接。

3.ususally在MYSQL中,我們將創建DNS.but如何提及SQL server2005中的DNS。

這是什麼意思?

4.如果我想插入變量值(它將顯示在控制檯窗口中)到database.kindly讓我知道這有可能嗎?如果是的話,建議我實施它的任何想法。

您可以在示例中使用帶有AddNew或執行功能的ADO RecordSet。使用Execute可以將adCmdText添加到最後一個參數。