数据库连接字符串的写法:
访问ODBC数据 |
"Provider=MSDASQL;DSN=dsnName;UID=userName;PWD=userPassword;" |
访问Oracle数据库 |
"Provider=MSDAORA;Data Source=serverName;User ID=userName; Password=userPassword;" |
访问MS SQL数据库 |
"Provider=SQLOLEDB;Data Source=serverName;Initial Catalog=databaseName; User ID=userName;Password=userPassword;" |
访问ACCESS 数据库 |
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=databaseName;User ID=userName;Password=userPassword;" |
以下就是进行数据库的连接,由于ADO支持的数据库种类繁多,因此连接也相当复杂。从连接类型可以分为两种:1.采用连接字符串;2.使用UDL文件。对于每种方法又有不同的写法。
①.连接字符串
===>Access Connect String
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=dbtest.mdb
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=dbTest.mdb;Jet OLEDB:Database Password=********;");
===>SQLServer Connect String
Driver=SQL Server;Database=fwbmk;Server=127.0.0.1;UID=sa;PWD=xx; (自带)
使用标准安全级别:
strConnect = _T("Provider=sqloledb;Data Source=MyServerName;Initial Catalog=MyDatabaseName;User Id=MyUsername;Password=MyPassword;");
使用信任连接:
strConnect = _T("Provider=sqloledb;Data Source=MyServerName;Initial Catalog=MyDatabaseName;Integrated Security=SSPI;");
指定SQLServer实例:
strConnect = _T("Provider=sqloledb;Data Source=MyServerName/MyInstanceName;Initial Catalog=MyDatabaseName;User Id=MyUsername;Password=MyPassword;");
本地数据库:
strConnect = _T("Provider=sqloledb;Data Source=(local);Initial Catalog=myDatabaseName;User ID=myUsername;Password=myPassword;");
网络数据库:
strConnect = _T("Provider=sqloledb;Network Library=DBMSSOCN;Data Source=130.120.110.001,1433;Initial Catalog=MyDatabaseName;User ID=MyUsername;Password=MyPassword;");
3
计算机软件技术《数据库部分》——ADO开发应用程序
===>oracle Connect String
微软提供的Oracle标准连接是:
strConnect = _T("Provider=MSDAORA;Data Source=serverName;User ID=userName; Password=userPassword;");
Oracle公司提供的连接方式:
使用标准安全级别:
strConnect = _T("Provider=OraOLEDB.Oracle;Data Source=MyOracleDB;User Id=myUsername;Password=myPassword;");
使用信任连接
1.strConnect = _T("Provider=OraOLEDB.Oracle;Data Source=MyOracleDB;User Id=/;Password=;"); UID为'/'
2.strConnect = _T("Provider=OraOLEDB.Oracle;Data Source=MyOracleDB;OSAuthent=1;");使用OSAuthent=1
对于连接字符串可以参考以下网页: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ado270/htm/mdreforacleprovspec.asp?frame=true
http://download-west.oracle.com/otndoc/oracle9i/901_doc/win.901/a90171/using.htm
===>ODBC 配置数据源
strConnect = _T("DSN=ADOTest");
http://www.able-consulting.com/ado_conn.htm
2.使用UDL文件
新建一个文本文档,然后后缀名改成: MyConn.udl 就可以了
void ADOConn::OnInitADOConn()
{
// initialization COM;
::CoInitialize(NULL);
HRESULT hr=m_pConnection.CreateInstance(__uuidof(Connection));
if(SUCCEEDED(hr))
try
{
}
catch(_com_error e)//
{
e.Description();
e.ErrorMessage();
e.Source();
}
//打开数据库
m_pConnection.CreateInstance(__uuidof(Connection));
m_pConnection->ConnectionString =”File Name=d:\\vc\\MyConn.udl”;
try
{
m_pConnection->Open(L”",L”",L”",-1);
//或者去掉上面那句,直接用m_pConnection->Open(L”File Name=D:\\vc\\MyConn.udl”,L”",L”",-1);
}
catch(_com_error e)//排除异常
{
CString errormessage;
errormessage.Format(”Open Dababase Faile!\r\nFaile Information:%s”, e.ErrorMessage());
AfxMessageBox(errormessage);//显示出错信息!
}
//AfxMessageBox(”opened Database! “);
版权声明:
此文为本站源创文章[或由本站编辑从网络整理改编],
转载请备注出处:
[CSDN]
http://blog.csdn.net/xgx198831/article/details/6975814
[若此文确切存在侵权,请联系本站管理员进行删除!]
--THE END--