用SQLConfigDataSource动态建立与SQL SERVER 的DSN是,默认的身份认证是:使用用户登陆ID和密码的SQL SERVER。所以在连接时老是出错。请问如何在建立DSN时将身份验证设为:使用网络登陆ID的WINDOWS 验证。或者在默认情况下的连接用什么UID,PSW。
程序如下:
char    *szDriver = "SQL Server";
char    *szAttributes = "DSN=SQL\0SERVER=.\0Description=人事管理系统\0DATABASE=test\0";
if(!SQLConfigDataSource(NULL, ODBC_ADD_SYS_DSN, szDriver, szAttributes))
{
AfxMessageBox(_T("创建数据源失败!"));
return false;
}
try
{
m_pConnection.CreateInstance("ADODB.CONNECTION");//设置连接对象
m_pConnection->ConnectionTimeout = 3;//设置连接超时为3秒
m_pConnection->Open("Provider=MSDASQL; Data Source=SQL", "", "", adConnectUnspecified);
}

解决方案 »

  1.   

    已解决,再问个问题:
    如何通过VC在已知数据库中创建数据表?(ADO)
    如果能给点代码就最好了!^_^
      

  2.   

    BOOL CDlgTest::CreateAccessTable()
    {
    _bstr_t strSQL;
    CString strTemSQL;
    CString str, str1, str2, str3;
    CString strTabName,strName;

    CStringArray stt;
    CStringArray stTabName; int i,n;
    HRESULT hr(-1);    UpdateData(TRUE);
    //OnSelchangeComboType();
     n=m_List.GetItemCount();
     stt.SetSize(n);
    for (i=0;i<n;i++)
    {
    if(m_List.GetItemText(i,1)=="Text")
    {
    strTemSQL=m_List.GetItemText(i,0) + " " +
    m_List.GetItemText(i,1) + "(" +
    m_List.GetItemText(i,2) + ")" + ",";
    }
    else if (m_List.GetItemText(i,1)=="Autoincremnet")
    {
    strTemSQL=m_List.GetItemText(i,0) + " " +
    m_List.GetItemText(i,1) + ",";
    }
    else if (m_List.GetItemText(i,1)=="Currency")
    {
    strTemSQL=m_List.GetItemText(i,0) + " " +
    m_List.GetItemText(i,1) + ",";

    }
    else if(m_List.GetItemText(i,1)=="DateTime")
    {
    strTemSQL=m_List.GetItemText(i,0) + " " +
    m_List.GetItemText(i,1) + ",";
    }
    else if(m_List.GetItemText(i,1)=="Text")
    {
    strTemSQL=m_List.GetItemText(i,0) + " " +
    m_List.GetItemText(i,1) + "(" +
    m_List.GetItemText(i,2) + ")" + ",";
    }
    else if(m_List.GetItemText(i,1)=="Double")
    {
    strTemSQL=m_List.GetItemText(i,0) + " " +
    m_List.GetItemText(i,1) + ",";
    }
    else if(m_List.GetItemText(i,1)=="Decimal")
    {
    strTemSQL=m_List.GetItemText(i,0) + " " +
    m_List.GetItemText(i,1) + "(" +
    m_List.GetItemText(i,2) + "," + 
    m_List.GetItemText(i,3) + ")" + ",";
    }
    else if(m_List.GetItemText(i,1)=="Single")
    {
    strTemSQL=m_List.GetItemText(i,0) + " " +
    m_List.GetItemText(i,1) + ",";
    }
    else if(m_List.GetItemText(i,1)=="Integer")
    {
    strTemSQL=m_List.GetItemText(i,0) + " " +
    m_List.GetItemText(i,1) + ","; }
    /* else if (m_List.GetItemText(i,1)=="Yes")
    {
    strTemSQL=m_List.GetItemText(i,0) + " " +
    m_List.GetItemText(i,1) + ",";
    }*/
    stt[i] = strTemSQL; }
    str2 = m_List.GetItemText(0,0);
    str3 = "";
    for(i=0;i<n;i++)
    {
    if(ListView_GetCheckState(m_List,i))
    {

    if(i>0)
    {
    str1 = m_List.GetItemText(i,0); str.Format("%s,%s",str2,str1);
    str2 = str;
    }
    str.Format("%s%s",str3,stt[i]);
    str3 = str;
    }
    }


    // BOOL bOpenFileDialog= false;
    // CString filepath;
    // CString m_strResult;
    // CString strName;
    // LPCTSTR lpszDefExt = _T("");
    // LPCTSTR lpszFileName= _T("tabAccess");
    // //DWORD dwFlags = OFN_HIDEREADONLY;
    // DWORD dwFlags = OFN_OVERWRITEPROMPT;
    //
    //
    // LPCTSTR lpszFilter = _T("(*.*)|*.*|");
    //
    // CFileDialog dlg(bOpenFileDialog,
    // lpszDefExt,lpszFileName,
    // dwFlags,lpszFilter,CWnd::FromHandle(hMainWnd));
    //  dlg.m_ofn.lpstrTitle = _T("&Ecirc;&yacute;&frac34;&Yacute;&iquest;&acirc;&Icirc;&Auml;&frac14;&thorn;");
    // if(dlg.DoModal()==IDOK)
    // {
    // filepath=dlg.GetPathName();
    // strName=dlg.GetFileName();
    //
    // UpdateData(FALSE);
    // }
    try
    {
    m_pRecordset=theApp.m_pConnection->OpenSchema(adSchemaTables);
    while(!(m_pRecordset->adoEOF))
    {
    //&raquo;&ntilde;&Egrave;&iexcl;±í&cedil;&ntilde;    
    _bstr_t table_name = m_pRecordset->Fields->GetItem("TABLE_NAME")->Value;
    //&raquo;&ntilde;&Egrave;&iexcl;±í&cedil;&ntilde;&Agrave;à&ETH;&Iacute;        
    _bstr_t table_type = m_pRecordset->Fields->GetItem("TABLE_TYPE")->Value;


    //&sup1;&yacute;&Acirc;&Euml;&Ograve;&raquo;&Iuml;&Acirc;&pound;&not;&Ouml;&raquo;&Ecirc;&auml;&sup3;&ouml;±í&cedil;&ntilde;&Atilde;&ucirc;&sup3;&AElig;&pound;&not;&AElig;&auml;&Euml;&ucirc;&micro;&Auml;&Ecirc;&iexcl;&Acirc;&Ocirc;

    if ( strcmp(((LPCSTR)table_type),"TABLE")==0)
    {
    strTabName=(LPCSTR)table_name;
    if(m_strTabName=="")
    {
    ::MessageBox(NULL,"±í&Atilde;&ucirc;&sup2;&raquo;&Auml;&Uuml;&Icirc;&ordf;&iquest;&Otilde;,&Ccedil;&euml;&Ecirc;&auml;&Egrave;&euml;±í&Atilde;&ucirc;","&Igrave;á&Ecirc;&frac34;",MB_OK);

    return false;
    } else if(strTabName==m_strTabName)
    {
    strName.Format("±í&Atilde;&ucirc;%s&Ograve;&Ntilde;&frac34;&shy;&acute;&aelig;&Ocirc;&Uacute;,&Ccedil;&euml;&Ouml;&Oslash;&ETH;&Acirc;&Ecirc;&auml;&Egrave;&euml;!",m_strTabName);
    ::MessageBox(NULL,strName,"&Igrave;á&Ecirc;&frac34;",MB_OK);
         return false;
    }
    }       
    m_pRecordset->MoveNext();  
    }

    strName=m_strTabName;
    }
    catch(_com_error e)///&sup2;&para;×&frac12;&Ograve;ì&sup3;&pound; 
    {  
    ::MessageBox(NULL,e.Description(),"&Igrave;á&Ecirc;&frac34;",MB_OK);
    return FALSE;
    }
    str.Format("CREATE TABLE %s (ID INTEGER,X decimal(10,2),Y decimal(10,2),Z decimal(10,2),%s CONSTRAINT MyTableConstraint PRIMARY KEY(ID),UNIQUE(X,Y,Z,%s))", strName,str3, str2);
    // str = "CREATE TABLE tabAccess (ID INTEGER,X F(10,2),Y F(10,2),Z F(10,2), CONSTRAINT MyTableConstraint PRIMARY KEY(ID),  UNIQUE(X,Y,Z))";
    // str = "CREATE TABLE %s (ID INTEGER,X Number,Y Double,Z Double, CONSTRAINT MyTableConstraint PRIMARY KEY(ID),  UNIQUE(X,Y,Z))";

    strSQL= str;
    try
    {

    theApp.m_pConnection->Execute(strSQL,NULL,adCmdText);
    //theApp.m_pConnection->Execute(strTemSQL,NULL,adCmdText);

    }
    // hr = (HRESULT)0;
    catch (_com_error &e) 
    {
    ::MessageBox(NULL,e.Description(),"&Igrave;á&Ecirc;&frac34;",MB_OK);

    //hr = e.WCodeToHRESULT(e.WCode());

                return FALSE;
    }

    //::MessageBox(NULL,"&acute;&acute;&frac12;¨±í&sup3;&Eacute;&sup1;&brvbar;&pound;&iexcl;","&Igrave;á&Ecirc;&frac34;",MB_OK);
    UINT j=::MessageBox(NULL,"&acute;&acute;&frac12;¨±í&sup3;&Eacute;&sup1;&brvbar;&pound;&iexcl;","&Igrave;á&Ecirc;&frac34;",MB_OK);;
    if(j==IDOK)
    {
    UINT i=::MessageBox(NULL,"&Ecirc;&Ccedil;·&ntilde;&Iuml;&Ouml;&Ocirc;&Uacute;&sup2;é&iquest;&acute;±í&frac12;á&sup1;&sup1;","&Igrave;á&Ecirc;&frac34;",MB_YESNO);
    if(i==IDYES)
    {
    UpdateData(true);
    CString str="";
    /* int ncount=rstab.m_cmbTable.GetCount();
    for(int num=0;num<ncount;num++)
    {
        rstab.m_cmbTable.GetLBText(num,m_strTabName);
    }*/
    CDlgRecordset dlg(this);
    dlg.m_strCMBTable = m_strTabName;
    /* int ncount=dlg.m_cmbTable.GetCount();
    for(int num=0;num<ncount;num++)
    {
    dlg.m_cmbTable.GetLBText(num,str);
    if(str==m_strTabName)
    {
    str=m_strTabName;
    break;
    }
    }*/
    /// dlg.m_cmbTable.SetCurSel(num);
    dlg.DoModal();
    }
    else
    {
    CDlgTest dlg(this);
    dlg.DoModal();
    }
    }
    return TRUE;}
      

  3.   

    用代码实现自动注册一个工程目录下的access数据库的ODBC数据源
    http://blog.csdn.net/laiyiling/archive/2004/08/28/87654.aspx
    创建数据库表同样是执行SQL语句,用_CommandPtr执行语句就可以了,参考
    http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=201018
      

  4.   

    CString  string;
      string.Format("ODBC;DRIVER={SQL Server};SERVER=%s;UID=%s;PWD=%s", "(local)", "sa", "");
      Database->Open(NULL, FALSE, FALSE, string);
    Database->ExecuteSQL("create database image");
    Database->ExecuteSQL(建表的sql语句);
      Database->Close();