none
about how to use the VS2012 to link sqlserver 2012 RRS feed

  • 问题

  • //
    #pragma once
    #include "stdafx.h"
    #include <stdio.h>
    #include <tchar.h>
    #import "c:\Program Files\Common Files\System\ADO\msado15.dll"     no_namespace rename("EOF", "EndOfFile")
    #define WIN32_LEAN_AND_MEAN
     using namespace std;
    
    int main(int argc, _TCHAR* argv[])
    {
    char * sqlCommand = "delete  from Student_Data where name='12345'"; //test 
     _bstr_t strConnect = "DSN=Student_ODBC;Server=WIN-SERVER2012\\SERVER2012;Database=SEC1202;uid=sa;pwd=woaini1314.;";
    
     ::CoInitialize(NULL);//初始化COM库
     
      //添加一个指向Connection对象的指针m_pConnection
      _ConnectionPtr m_pConnection(__uuidof(Connection));
    
      if( FAILED( m_pConnection.CreateInstance(__uuidof(Connection)) ) ) //创键Connection对象
      {
       cout <<"Error1!\n"
            <<endl;
      }
     try
     {
       m_pConnection->Open(strConnect,"","", adModeUnknown); //连接数据库
     }
     catch(_com_error e) 
     {
      cout << "sorry ! I can not link the database!Palease try agian!"
       << endl;
      getchar();
     }
      m_pConnection->Execute(sqlCommand, NULL, 1);//执行SQL语句
      m_pConnection->Close();
      ::CoUninitialize(); //释放程序占用的COM 资源
    
     return 0;
    }
    

    Who can help me.
    2013年9月2日 15:07

答案

  • Did you test sql connection with that dsn in odbc? Got any error when you run above code?
    2013年9月2日 15:33
  • 你好,

    正如@rmiao所说的,首先你可以测试一下SQL连接是否成功,如果连接不成功,那么就检查一下sql 连接字符串是否正确;如果不正确,为了更好的定位错误,你可以把错误的信息发到论坛。

    这里有一段用VC++连接SQL数据库的代码,你可以参考一下:

    CString sSQLStmt ="SELECT * FROM aaa";
     int bconnect;
    
     CString str = "ODBC;DSN=xxxxx;UID=sa;PWD=pass;" ; 
    
     bconnect=cdb.Open(str);
     if(!bconnect)
     printf("failed");
    
     else 
     printf("connection open successfully\n");
    
     CRecordset rs(&cdb);
    
     CString varValue;
    
     CDBVariant varValue1;
    
     _bstr_t eventTime;
    
     rs.Open( CRecordset::forwardOnly, sSQLStmt );
    
     short nFields = rs.GetODBCFieldCount( );
     while( !rs.IsEOF( ) )
     {
    
     rs.GetFieldValue( "[column name]", varValue);
     eventTime = varValue;
     printf("eventTime: %s\n\n", varValue);
     rs.MoveNext( );
    
     }
     rs.Close( );
    

    更多信息可以参考:

    # ODBC connection

    http://www.cplusplus.com/forum/windows/2159/

    # ODBC Connections

    http://msdn.microsoft.com/en-us/library/79hh5st2.aspx

    # OdbcConnection::ConnectionString Property

    http://msdn.microsoft.com/en-us/library/system.data.odbc.odbcconnection.connectionstring.aspx?cs-save-lang=1&cs-lang=cpp#code-snippet-1

     

    Best Regards,

    Lisa Zhang


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2013年9月3日 12:55
    版主

全部回复

  • Did you test sql connection with that dsn in odbc? Got any error when you run above code?
    2013年9月2日 15:33
  • 遇到的错误信息是什么?


    Please Mark As Answer if it is helpful.

    2013年9月3日 1:52
  • 你好,

    正如@rmiao所说的,首先你可以测试一下SQL连接是否成功,如果连接不成功,那么就检查一下sql 连接字符串是否正确;如果不正确,为了更好的定位错误,你可以把错误的信息发到论坛。

    这里有一段用VC++连接SQL数据库的代码,你可以参考一下:

    CString sSQLStmt ="SELECT * FROM aaa";
     int bconnect;
    
     CString str = "ODBC;DSN=xxxxx;UID=sa;PWD=pass;" ; 
    
     bconnect=cdb.Open(str);
     if(!bconnect)
     printf("failed");
    
     else 
     printf("connection open successfully\n");
    
     CRecordset rs(&cdb);
    
     CString varValue;
    
     CDBVariant varValue1;
    
     _bstr_t eventTime;
    
     rs.Open( CRecordset::forwardOnly, sSQLStmt );
    
     short nFields = rs.GetODBCFieldCount( );
     while( !rs.IsEOF( ) )
     {
    
     rs.GetFieldValue( "[column name]", varValue);
     eventTime = varValue;
     printf("eventTime: %s\n\n", varValue);
     rs.MoveNext( );
    
     }
     rs.Close( );
    

    更多信息可以参考:

    # ODBC connection

    http://www.cplusplus.com/forum/windows/2159/

    # ODBC Connections

    http://msdn.microsoft.com/en-us/library/79hh5st2.aspx

    # OdbcConnection::ConnectionString Property

    http://msdn.microsoft.com/en-us/library/system.data.odbc.odbcconnection.connectionstring.aspx?cs-save-lang=1&cs-lang=cpp#code-snippet-1

     

    Best Regards,

    Lisa Zhang


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2013年9月3日 12:55
    版主