HRESULT Connection15::
Open
(_bstr_t ConnectionString, _bstr_t UserID, _bstr_t Password, long Options )
ConnectionString为连接字串,UserID是用户名, Password是登陆密码,Options是连接选项,用于指定Connection对象对数据的更新许可权。
看到常用的有两种方法:
(1)
通过DSN数据源对任何支持ODBC的数据库进行连接:
m_pConnection->Open("Data Source=adotest;UID=sa;PWD=;","","",adModeUnknown); (2)
不通过DSN对SQL SERVER数据库进行连接: 
m_pConnection->
Open(
"driver={SQLServer};Server=127.0.0.1;DATABASE=vckbase;UID=sa;PWD=139","","",adModeUnknown
);//我也看到很多是把driver写成Provider的,为什么呢?
其中Server是SQL服务器的名称,DATABASE是库的名称其中第一个参数ConnectionString究竟该怎么写啊,为什么有的用户名UID=sa;要赋值,有的密码又不用赋值PWD=;。而且函数原型中已经说了第二个参数和地三个参数是用户名和密码,为什么还要在第一个参数中添加UID=sa;PWD=139这两个呢,而第二个和地三个参数却为空?各位高手,帮帮忙啊。
(SQL服务器用的是windows的验证登录的,自己并没有设置用户名和密码,而且我用的是上面的第二种方法)

解决方案 »

  1.   

    ADO中_ConnectionPtr的Open函数参数的写法
      (1)通过JET数据库引擎对ACCESS2000数据库的连接 
         m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\test.mdb","","",adModeUnknown); 
      (2)通过DSN数据源对任何支持ODBC的数据库进行连接: 
         m_pConnection->Open("Data Source=adotest;UID=sa;PWD=;","","",adModeUnknown); 
      (3)不通过DSN对SQL SERVER数据库进行连接:  
          m_pConnection->Open("driver={SQL      Server};Server=127.0.0.1;DATABASE=vckbase;UID=sa;PWD=139","","",adModeUnknown); 
         其中Server是SQL服务器的名称,DATABASE是库的名称用户名与密码需不需要赋值视你的数据库而定,如果你的数据库有密码,就需要赋值。    
      

  2.   

    在连接时ConnectionString可以传递帐号与密码,但你想如果是select,update,insert时,你需要权限来进行这些操作,就可以给第二,第三个参数赋值帐号与密码了。
      

  3.   

    看看帮助文件吧。
    Open Method (Connection) ADO
    Opens a connection to a data source. Applies To Connection Syntax connection.Open ConnectionString, UserID, Password Parameters ConnectionString Optional. A String containing connection information. See the ConnectionString property for details on valid settings. UserID Optional. A String containing a user name to use when establishing the connection. Password Optional. A String containing a password to use when establishing the connection. Res Using the Open method on a Connection object establishes the physical connection to a data source. After this method successfully completes, the connection is live and you can issue commands against it and process results. Use the optional ConnectionString argument to specify a connection string containing a series of argument = value statements separated by semicolons. The ConnectionString property automatically inherits the value used for the ConnectionString argument. Therefore, you can either set the ConnectionString property of the Connection object before opening it, or use the ConnectionString argument to set or override the current connection parameters during the Open method call. If you pass user and password information both in the ConnectionString argument and in the optional UserID and Password arguments, the results may be unpredictable; you should only pass such information in either the ConnectionString argument or the UserID and Password arguments. When you have concluded your operations over an open Connection, use the Close method to free any associated system resources. Closing an object does not remove it from memory; you may change its property settings and use the Open method to open it again later. To completely eliminate an object from memory, set the object variable to Nothing. Remote Data Service Usage When used on a client-side Connection object, the Open method doesn’t actually establish a connection to the server until a Recordset is opened on the Connection object. 
      

  4.   

    Contains the information used to establish a connection to a data source. Applies To Connection Settings and Return Values Sets or returns a String value. Res Use the ConnectionString property to specify a data source by passing a detailed connection string containing a series of argument = value statements separated by semicolons. ADO supports seven arguments for the ConnectionString property; any other arguments pass directly to the provider without any processing by ADO. The arguments ADO supports are as follows: Argument Description 
     
    Provider= Specifies the name of a provider to use for the connection. 
    Data Source= Specifies the name of a data source for the connection, for example, a SQL Server database registered as an ODBC data source. 
    User ID= Specifies the user name to use when opening the connection. 
    Password= Specifies the password to use when opening the connection. 
    File Name= Specifies the name of a provider-specific file (for example, a persisted data source object) containing preset connection information. 
    Remote Provider= Specifies the name of a provider to use when opening a client-side connection. (Remote Data Service only.) 
    Remote Server= Specifies the path name of the sever to use when opening a client-side connection. (Remote Data Service only.) After you set the ConnectionString property and open the Connection object, the provider may alter the contents of the property, for example, by mapping the ADO-defined argument names to their provider equivalents. The ConnectionString property automatically inherits the value used for the ConnectionString argument of the Open method, so you can override the current ConnectionString property during the Open method call. Because the File Name argument causes ADO to load the associated provider, you cannot pass both the Provider and File Name arguments. The ConnectionString property is read/write when the connection is closed and read-only when it is open. Remote Data Service Usage When used on a client-side Connection object, the ConnectionString property can only include the Remote Provider and Remote Server parameters. ----------------------------
    你只给第一个参数其实就可以了。
      

  5.   

    这个字符串用ado控件可以自动生成。
      

  6.   

    用ADO data控件生成的ConnectionString为如下:
    _bstr_t strConnect = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Test;Data Source=8045DEA9E2074E5\\HZY";
    用该连接字符串能够成功连接,但是为什么跟上面所说 (1)(2)两种方法中的ConnectionString参数有很大的差别呢。而且如果我不用ADO data控件的话又该怎么去获得这个ConnectionString呢?