2012-02-15 91 views
1

我在MSDN网站上找到了一个很好的例子。 SQLConnect什么是正确的ConnectionString传递SQLConnect?

我想连接到SQL服务器,连接到我的数据库,但不幸的是,SQLConnect总是返回-1,并且应用程序冻结。

我已编辑上面的代码:

direxec::direxec() 
{ 
_mbscpy_s(chr_ds_name, MAX_DATA, (const unsigned char *)"Server=mySQLServer;Database=myDatabaseName;"); 
_mbscpy_s(uid, MAX_DATA, (const unsigned char *)"testuser"); 
_mbscpy_s(pwd, MAX_DATA, (const unsigned char *)"testpassword"); 
printf("\n%s",chr_ds_name); 
} 

void direxec::sqlconn() 
{  
SQLAllocEnv(&henv); 
SQLAllocConnect(henv, &hdbc); 

rc = SQLConnect(hdbc, chr_ds_name, SQL_NTS, uid, SQL_NTS, pwd, SQL_NTS); 

// Deallocate handles, display error message, and exit. 
if (!MYSQLSUCCESS(rc)) 
{ 
    printf("\nFailed %d",rc); 
    SQLFreeConnect(henv); 
    SQLFreeEnv(henv); 
    SQLFreeConnect(hdbc); 
    if (hstmt) error_out(); 
    exit(-1); 
} 
printf("\nConnected"); 
rc = SQLAllocStmt(hdbc, &hstmt); 
} 

使用我的SQLDriverConnect可以连接到我的数据库,但我想使用的SQLConnect如果可能的话。 有没有人有任何想法我做错了什么?

谢谢!

回答

1

SqlConnect不接受连接字符串,它只接受数据源名称(DSN)。在管理工具中的ODBC管理工具中配置DSN。如果你想使用连接字符串,那么你需要使用SqlDriverConnect

+0

啊,好吧,我现在明白了。谢谢。 – kampi 2012-02-15 07:05:50

相关问题