ADO连接的用户名和密码问题
在用C++编写数据库系统时,我试图用ADO技术进行与SQL SERVER的连接。用ADO技术进行连接时,需要在连接字符串中写出用户名和密码。在我平时登陆SQLSERVER时,是直接通过WINDOWS验证的,根本不需要输入用户名和密码。现在在程序中连接,需要用户名和密码,这可真把我难住了。因为我不知道我平常登陆时系统默认的用户名和密码是什么。
#include"stdio.h"
#import "c:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF") rename("BOF","adoBOF")
_ConnectionPtr m_pConnection;
int main(){
try{
m_pConnection.CreateInstance("ADODB.Connection");
_bstr_t strConnect="Provider=SQLOLEDB.1;Password=;Persist Security Info=True;User ID=;Initial Catalog=SC;Data Source=localhost;";
m_pConnection->Open(strConnect,"","",adModeUnknown);
if(m_pConnection->State==adStateOpen){
printf("OK\n");
while(getchar()=='\n');
}//if
else{
printf("连接失败!\n");
while(getchar()=='\n');
}//else
m_pConnection->Close();
if(m_pConnection->State==adStateClosed){
printf("已断开连接!\n");
while(getchar()=='\n');
}//if
}
catch(_com_error e){
printf("出现异常!\n");
while(getchar()=='\n');
}
printf("OK\n");
while(getchar()=='\n');
}
这是我进行连接实验时的代码。在连接字符串 _bstr_t strConnect="Provider=SQLOLEDB.1;Password=;Persist Security Info=True;User ID=;Initial Catalog=SC;Data Source=localhost;";中,无论USER ID和PASSWORD怎么改,运行结果都是输出“出现异常!”。根据我这程序的代码,这样的输出意味着根本没连上SQLSERVER。 sa和其密码 忘了的话在企业管理器里可以改 怎么改? sql server组- >安全性- >登录 谢谢皇家救星!经过一天的奋斗,我终于成功连接数据库了。连接的代码是:
void Connect(){
try{
CoInitialize(NULL);
_RecordsetPtr m_pRecordset(_uuidof(Recordset));
_ConnectionPtr m_pConnection("ADODB.Connection");
m_pConnection->Open("Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=library;Data Source=YEQIJING","","",adConnectUnspecified);
if(m_pConnection->State==adStateOpen){printf("Connect OK!\n"); return;}
else {printf("Fail to connect the database!\n");return;}
}//try
catch(_com_error e){
printf("Have execptions!\n");
}//catch
}//Connect()
但是现在又遇到新麻烦了:这样连接的话可移植性很差啊!因为在我的连接字符串里,Data Source=YEQIJING,其中YEQIJING是服务器名称。
在自家电脑上,这样连接没有问题。但是当软件交给客户后,客户的服务器名称可不是YEQIJING了。那样的话,这个程序在运行时就会产生连接数据库的作用。究竟怎么样才能有好的可移植性呢? (local) 或者把数据库源名写在配置文件里面 哪里改成local?我试过改Data Source=local,结果出现异常。
或者把数据库源名写在配置文件里面——也就是改哪一项呢?哪一项是配置文件?数据库源名又应该是一个什么样的名? 少了括号
配置文件:即将某些参数放在文件里面,程序启动时从里面获取参数
详细情况请搜索:VC 读取ini 改成这样还是异常
#import "c:\Program Files\Common Files\System\ado\msado15.dll"no_namespace rename("EOF","adoEOF") rename("BOF","adoBOF")
#include"stdio.h"
#include"string.h"
_RecordsetPtr m_pRecordset;
_ConnectionPtr m_pConnection;
void Connect(){
try{
CoInitialize(NULL);
m_pRecordset.CreateInstance(_uuidof(Recordset));
m_pConnection.CreateInstance("ADODB.Connection");
m_pConnection->Open("Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=library;Data Source=(local)","","",adModeUnknown);
if(m_pConnection->State==adStateOpen){printf("Connect OK!\n"); return;}
else {printf("Fail to connect the database!\n");return;}
}//try
catch(_com_error e){
printf("Have execptions!\n");
}//catch
}//Connect()
void Disconnect(){
m_pConnection->Close();
if(m_pConnection->State==adStateClosed)
printf("Disconnect OK!\n");
else
printf("Fail to disconnect!\n");
}//Disconnect()
int main(){
Connect();
_bstr_t vSQL;
vSQL="SELECT * FROM CLERK";
m_pRecordset->Open(vSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
for(m_pRecordset->MoveFirst();!m_pRecordset->adoEOF;m_pRecordset->MoveNext()){
char ID;
char Name;
strcpy(ID,(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("CLERK_ID"));
strcpy(Name,(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("NAME"));
printf("ID:%s NAME:%s\n",ID,Name);
}//for()
while(getchar()=='\n');
Disconnect();
while(getchar()=='\n');
}//main() Data Source=后面似乎非要接YEQIJING不可。 那就搞成配置文件吧 把有可能变动的都写在配置文件里面 到时到别人机上改一下配置文件就行 exe文件不用动 现在很多软件都是这样搞 听说以后的趋向是用xml代替ini 在我和同学机上都有(local)这个服务器名 我们都是装的sql2000 不知道为何你的没有 我是SQL SERVER2005
我后来用STRCAT函数OK了。其中服务器名需要用户自己输入。
页:
[1]