strCn = "Provider=MySQLProv;Data Source=localhost;UserId=root;Password=12345;" ;
_variant_t bcnstr = _variant_t(strCn);
_variant_t bunstr = _variant_t(UserName);
_variant_t bpwdstr = _variant_t(Pwd);
//打开一个连接m_Conn->Open(_bstr_t(bcnstr),_bstr_t(bunstr),
_bstr_t(bpwdstr), adOpenUnspecified);//adOpenUnspecified代码很简单,其中,用户名,密码都是正确的。但是就是连接不上,为什么?
我连接SQLSERVER是成功的。但是MYSQL却失败,请问我的代码有问题吗?
正确的应该怎么写?

解决方案 »

  1.   

    试试下面连接里的。
    http://www.codeproject.com/database/connectionstrings.asp
      

  2.   

    由于ADO对象不直接支持MySQL,所以必须先安装MyODBC
      

  3.   

    下面是连接SQL Server得
    改动一下连接字符穿
    strSQL="Provider=SQLOLEDB;Server=Gooyan;DataBase=SkillsBox;UID=sa;PWD=aaaaaaaa";
    1.stdafx.h中加入
    #import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF") 
    2。app文件的InitInstance中加入
    if(S_OK!=OleInitialize(NULL))
    {
    AfxMessageBox("初始化COM组件库错误");
    }
    下面就是数据库操作
    _ConnectionPtr pConn;
    _RecordsetPtr  pRs;
    CString strSQL;
    pConn.CreateInstance(__uuidof(Connection));
    pConn->CursorLocation=adUseClient;
    strSQL="Provider=SQLOLEDB;Server=Gooyan;DataBase=SkillsBox;UID=sa;PWD=aaaaaaaa";
    pConn->Open(_bstr_t(strSQL),"","",-1);pRs.CreateInstance(__uuidof(Recordset));
    pRs->CursorLocation=adUseClient;
    pRs->PutActiveConnection(pConn.GetInterfacePtr());pRs1.CreateInstance(__uuidof(Recordset));
    pRs1->CursorLocation=adUseClient;
    pRs1->PutActiveConnection(pConn.GetInterfacePtr());strSQL="select * from catalog order by cataid";
    pRs->Open(_bstr_t(strSQL),vtMissing,adOpenDynamic,adLockBatchOptimistic,adCmdText);
      

  4.   

    谢谢gooyan(超级替补) 的关心,
    我连接SQLSERVER本来就没问题,
    就是连不上MYSQL.
    连接string,我在帖子中已经给出,
    但就是无法连接,
    为什么?
      

  5.   

    ODBC Driver for MySQL
    If you want to connect to a local database, you can use a connection string like the following:strConnect = _T("Driver={MySQL ODBC 3.51 Driver};Server=localhost;"
         "Database=MyDatabase;User=MyUserName;Password=MyPassword;Option=4;");
    If you want to connect with a remote database, you need to specify the name of the server or its IP in the Server parameter. If the Port is distinct to 3306 (default port), you must specify it.strConnect = _T("Driver={mySQL ODBC 3.51 Driver};Server=MyRemoteHost;"
         "Port=3306;Option=4;Database=MyDatabase;Uid=MyUsername;Pwd=MyPassword;");
    The parameter Option can be one or more of the following values:1 - The client can't handle that MyODBC returns the real width of a column. 
    2 - The client can't handle that MySQL returns the true value of affected rows. If this flag is set then MySQL returns 'found rows' instead. One must have MySQL 3.21.14 or newer to get this to work. 
    4 - Make a debug log in c:\myodbc.log. This is the same as putting MYSQL_DEBUG=d:t:O,c::\myodbc.log in AUTOEXEC.BAT. 
    8 - Don't set any packet limit for results and parameters. 
    16 - Don't prompt for questions even if driver would like to prompt. 
    32 - Enable or disable the dynamic cursor support. This is not allowed in MyODBC 2.50. 
    64 - Ignore use of database name in 'database.table.column'. 
    128 - Force use of ODBC manager cursors (experimental). 
    256 - Disable the use of extended fetch (experimental). 
    512 - Pad CHAR fields to full column length. 
    1024 - SQLDescribeCol() will return fully qualified column names. 
    2048 - Use the compressed server/client protocol. 
    4096 - Tell server to ignore space after function name and before '(' (needed by PowerBuilder). This will make all function names keywords! 
    8192 - Connect with named pipes to a MySQLd server running on NT. 
    16384 - Change LONGLONG columns to INT columns (some applications can't handle LONGLONG). 
    32768 - Return 'user' as Table_qualifier and Table_owner from SQLTables (experimental). 
    65536 - Read parameters from the client and ODBC groups from my.cnf. 
    131072 - Add some extra safety checks (should not be needed but...). 
    If you want to have multiple options, you should add the above flags! For example: 16 + 1024 = 1030 and use Option= 1030;.For more information, go to MyODBC Reference Manual.
      

  6.   

    连接mysql可以用mysql的函数,很方便的,你可以去搜一下"MySQL中文参考手册",上面有函数的介绍.