编程论坛's Archiver

mxbwuma 发表于 2008-2-24 12:56

怎样用vc++连接SQL SERVER

//主要代码如下
::CoInitialize(NULL);
        try
        {
                HRESULT hr;
                m_pConnection.CreateInstance("ADODB.Connection");
                _bstr_t ConStr="Provider=SQLOLEDB;Password=;Persist Security Info=false;User ID=sa;Initial Catalog=data;Data Source=127.0.0.1";//请各位高手看看此处有没有不妥的地方,我就是这里搞不懂应该怎样填写才正确。[bc02]
                hr=m_pConnection->Open(ConStr,"","",NULL);
                if(SUCCEEDED(hr))
                {
                        AfxMessageBox("success");
                        m_pRecordset.CreateInstance(__uuidof(Recordset));
                    _bstr_t vSQL;
                        vSQL="select * from student";
                        m_pRecordset->Open(vSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
                        m_pRecordset->MoveFirst();
                        CString username;
                        username=(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("name");
                        AfxMessageBox(username);
                }
                else
                        AfxMessageBox("断开连接");
                m_pConnection->Close();
        }
        catch(_com_error e)
        {
                AfxMessageBox(e.ErrorMessage());
                AfxMessageBox(e.Description());
                AfxMessageBox(e.Source());
        }
        CoUninitialize();
运行结果:提示说用户sa连接不到数据库。我就不知道该怎么办了。
是不是在计算机上还要设置一些什么连接????[bc06]

mxbwuma 发表于 2008-2-24 14:10

补充:
User ID与Password分别指什么用户名与密码,它们是在哪里设置的?

skwenl 发表于 2008-2-25 08:45

user id是指登录sql 的用户名,password就是密码

mxbwuma 发表于 2008-2-25 09:37

噢,谢谢了!

页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.