ADO连接SQL数据库的问题C/C++ codetry{CoInitialize(NULL)_ConnectionPtrm_pConnectionm_pConnection.Cr
ADO 连接SQL 数据库的问题
C/C++ codetry { CoInitialize(NULL); _ConnectionPtr m_pConnection; m_pConnection.CreateInstance(__uuidof(_Connection)); m_pConnection->ConnectionString = "Provider=SQLOLEDB.1;Password=qxd5024278;Persist Security Info=False;User ID=qxd;Data Source=QIN-PC\SQLEXPRESS"; m_pConnection->Open("","","",NULL); } catch(_com_error e) { CString str = e.ErrorMessage(); MessageBox(str); }
结果提示无效指针。
小弟新手 求大牛帮忙
[解决办法]LZ这个问题最好到相应版块提问。答案会来的快些。
[解决办法]我这给个例子,把数据库相关信息写到link.ini文件里,
procedure TDM.DataModuleCreate(Sender: TObject);
var
AppIni:TInifile;
sFilename,sServer,sDatabase,sUsername,sPassword:string;
begin
//读取ini文件
sFilename:=ExtractFilePath(application.ExeName)+'link.ini' ;
AppIni := TIniFile.Create(sFilename);
sServer:=AppIni.ReadString('DataBase','Server','Server');
sDatabase:=AppIni.ReadString('DataBase','Database','Database');
sUsername:=AppIni.ReadString('DataBase','Username','Username');
sPassword:=AppIni.ReadString('DataBase','Password','Password');
//链接数据库
With con1 do
begin
Close;
LoginPrompt:=False;
ConnectionString:='Provider=SQLOLEDB.1;Password='#39+sPassword+#39';Persist Security Info=True;User ID='#39+sUsername+#39';Initial Catalog='#39+sDatabase+#39';Data Source='#39+sServer+#39'';
Try
Connected:=True;
Except
//链接异常处理
ON EOLEException do //这里需要在uses中添加ComObj才可以使用
begin
showmessage('数据库连接不成功!');
Halt;
end;
end;
end;
end;