希望能给出一个切实可用的例子或语句出来。
因为我看过MSDN后,自己在实现时,不知道如何处理UserID和PWD,如何填写,我试过总是失败。还有,就是SQL Server设置ODBC时,有几个步骤,如选择哪个数据库,这个该如何实现,因为默认的是master,而我用要的肯定不是这个master,谢谢。
BTW:开发环境是Visual C++

解决方案 »

  1.   

    SQLConfigDataSource(NULL,ODBC_ADD_DSN,
    (LPSTR)"SQL Server",
    (LPSTR)"DSN=dsnname\0"
    "SERVER=small\0"
    "DATABASE=databasename\0"))//添加一个ODBC数据源,其类型为
    //SQL Server,服务器为small,名字为dsnname,数据库为databasename
      

  2.   

    ODBC Driver for Sql ServerFor Standard Security:strConnection = _T("Driver={SQL Server};Server=MyServerName;Trusted_Connection=no;"
    "Database=MyDatabaseName;Uid=MyUserName;Pwd=MyPassword;");For Trusted Connection security: (Microsoft Windows NT integrated security)strConnection = _T("Driver={SQL Server};Server=MyServerName;Database=myDatabaseName;Uid=;Pwd=;");Also you can use the parameter Trusted_Connection that indicates that you are using the Microsoft Windows NT Authentication Mode to authorize user access to the SQL Server database. For example:strConnection = _T("Driver={SQL Server};Server=MyServerName;Database=MyDatabaseName;"
    "Trusted_Connection=yes;");If the Sql Server is running in the same computer you can replace the name of the server by the word (local) like in the following sample:strConnection = _T("Driver={SQL Server};Server=(local);"
    "Database=MyDatabaseName;Uid=MyUserName;Pwd=MyPassword;");If you want to connect with a remote SQL Server you must to inform the address, the port, and the Network Library to use: The Address Parameter must be an IP Address and must to include the portThe Network Parameter can be one of the following:    dbnmpntw Win32 Named Pipes    dbmssocn Win32 Winsock TCP/IP    dbmsspxn Win32 SPX/IPX    dbmsvinn Win32 Banyan Vines    dbmsrpcn Win32 Multi-Protocol (Windows RPC)
      

  3.   

    small_wei(small)的方法 是动态配置odbc数据源.
    至于连接时的口令,和用户id在open函数中.
      

  4.   

    上面给的方式是ODBC使用的情况,我要的是动态创建ODBC,即在数据源中创建它。这不一样。
    我的问题在于像small_wei(small)给出的,就差一个UID和PWD,然后,我直接加上"UID=sa\0WD=xxx\0",创建会失败.而这种用法,在对于mdb是可行的。所以,我很疑惑。
      

  5.   

    是的,Open中肯定要有UID和PWD,只是如果创建的时候不设的话,默认值是administrator,密码为空。
    我试试,是不是不管它也行....
      

  6.   

    我用过,是没问题的,用户名和密码是在使用时输入的,在数据源中输入是没有用的如:
    CDatabase
    db.Open("hs",false,false,"ODBC;UID=hs;PWD=hs")
      

  7.   

    呵呵,我也试了,的确是无关,那个数据源中的,只是一个默认值,反正我的程序中向来是自己写了UID和PWD,所以,Yeah!!! 这个问题,可以说困扰我很久了,只是我就那么木,没转过弯,老在数据源上死抠。真是失败!非常感谢两位。