就这点分了!大侠帮修改一下,万分感激!

freckyliao 2009-10-13 05:01:10
VC6.0 Win32 Dynamic-Link Library代码:
strCn="provider=SQLOLEDB;data source="+server
+";initial catalog="+db
+";userID="+UserName
+";Password="+Pwd;
_variant_t bcnstr=_variant_t(strCn);
_variant_t bunstr=_variant_t(UserName);
_variant_t bpwdstr=_variant_t(Pwd);

#include "gettime.h"
#include <iostream.h>
#include <comdef.h>
#include <atlbase.h>
#pragma warning(disable:4146)
#import "C:\Program Files\Common Files\System\ado\msado15.dll"no_namespace rename("EOF","adoEOF")
#pragma warning(default:4146)
#include <iomanip.h>
#include <string>
using namespace std;

__declspec(dllexport) LPTSTR GetTime()
{
string ServerName;
string UserID;
string Password;
string strCn;
::CoInitialize(NULL);
_ConnectionPtr pConn;
pConn.CreateInstance(__uuidof(Connection));
_RecordsetPtr pRs;
pRs.CreateInstance(__uuidof(Recordset));
_CommandPtr pCommand;
pCommand.CreateInstance(__uuidof(Command));

//open GetSVRDT.ini to get sql parameter
GetPrivateProfileString(NULL,"ServerName",NULL,ServerName.GetBuffer(MAX_PATH),MAX_PATH,".//GetSVRDT.ini");
GetPrivateProfileString(NULL,"UserID",NULL,ServerName.GetBuffer(MAX_PATH),MAX_PATH,".//GetSVRDT.ini");
GetPrivateProfileString(NULL,"Password",NULL,ServerName.GetBuffer(MAX_PATH),MAX_PATH,".//GetSVRDT.ini");
//pConn->Open(_bstr_t("Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;User ID=sa;Password=123623255;Initial Catalog=Northwind;Data Source=CNAECFUYD000072"),"","",adModeUnknown);
strCn="provider=SQLOLEDB;data source="+ServerName+";initial catalog="+db+";userID="+UserID+";Password="+Password+"";
_variant_t bcnstr=_variant_t(strCn);
_variant_t bunstr=_variant_t(UserName);
_variant_t bpwdstr=_variant_t(Pwd);
//string strConn="Provider=SQLOLEDB.1;Persist Security Info=True;Data Source="+ServerName+";UserID="+UserID+";Password="+Password+;
//pConn->Open(_bstr_t("Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa;Password=123623255;Initial Catalog=traceability;Data Source=cnaecfuyd000072"),"","",adModeUnknown);
pConn->Open(_bstr_t(strCn),"","",adModeUnknown);

pCommand->ActiveConnection=pConn;
pCommand->CommandText=_bstr_t("select convert(varchar(10),getdate(),110)+space(1)+convert(varchar(10),getdate(),108) as sysTime");
//pCommand->CommandText=_bstr_t("Select Convert(varchar(20),getdate(),120) AS sysTime");
pCommand->CommandType=adCmdText;
//pCommand->CommandTimeout=(long)300;
pCommand->Parameters->Refresh();

pRs=pCommand->Execute(NULL,NULL,adCmdUnknown);
_variant_t varValue=pRs->GetCollect("sysTime");
_bstr_t bstrValue=varValue.bstrVal;
LPTSTR lpBuffer = new TCHAR[100];
pRs->Close();
pConn->Close();
//lpBuffer=bstrValue;
//_stprintf(lpBuffer,_T("%s"),(LPCTSTR)bstrValue);
_stprintf(lpBuffer,_T("%s"),(LPCTSTR)bstrValue);
::CoUninitialize();
return lpBuffer;
}

代码是实现从SQL 服务器取时间,以String 型返回。
现在User ID;Password;Initial Catalog;Data Source要从server.ini 中取。
要怎么改呢?大侠帮忙改下。
...全文
108 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
frecky8504 2009-10-16
  • 打赏
  • 举报
回复
答案来了。感谢各位大佬帮忙,最后还是自己搞定了。
ini 文件:
[Connection]
SqlServerName=cnaecfuyd000072
SqlUSER=sa
SqlPWD=123623255
SqlDatabase=Northwind
char ServerName[200];
char UserID[200];
char Password[200];
char Database[200];
GetPrivateProfileString(_T("Connection"),_T("SqlServerName"),_T(""),ServerName,sizeof(ServerName),_T(".\\GetSVRDT.ini"));
GetPrivateProfileString(_T("Connection"),_T("SqlUSER"),_T(""),UserID,sizeof(UserID),_T(".\\GetSVRDT.ini"));
GetPrivateProfileString(_T("Connection"),_T("SqlPWD"),_T(""),Password,sizeof(Password),_T(".\\GetSVRDT.ini"));
GetPrivateProfileString(_T("Connection"),_T("SqlDatabase"),_T(""),Database,sizeof(Database),_T(".\\GetSVRDT.ini"));
string strConn;
strConn=strConn+"provider=SQLOLEDB.1;Persist Security Info=True;data source="+ServerName
+";Initial Catalog="+Database
+";User ID="+UserID
+";Password="+Password;
frecky8504 2009-10-15
  • 打赏
  • 举报
回复
上面的取ini文件有问题。
freckyliao 2009-10-15
  • 打赏
  • 举报
回复
没人解决?
freckyliao 2009-10-14
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 zhaohongbo83 的回复:]
GetPrivateProfileString("数据库", "主机名", NULL, m_szHost, sizeof(m_szHost), IniFile) ;
GetPrivateProfileString("数据库", "用户名", NULL, m_szUser, sizeof(m_szUser), IniFile) ;
GetPrivateProfileString("数据库", "密码", NULL, m_szPwd, sizeof(m_szPwd), IniFile) ;
GetPrivateProfileString("数据库", "默认库", NULL, m_szDef, sizeof(m_szDef), IniFile) ;

try
{
//连接XdData
HRESULT hr = m_Connection.CreateInstance(__uuidof(Connection));
sprintf(m_szConnect,"provider = sqloledb;server=%s;database=%s;", m_szHost, m_szDef);
hr=m_Connection->Open(_bstr_t(m_szConnect),_bstr_t(m_szUser),_bstr_t(m_szPwd),-1);
sprintf(m_szTmp, "数据库连接成功!");
//连接XdData
}
catch(_com_error & e)
{
sprintf(m_szTmp, "数据库打开失败,错误原因:%s\n",LPCTSTR(e.Description()));
MessageBox( NULL, m_szTmp,"失败提示",MB_OK );
return 0 ;
}
参考一下,

[/Quote]

我的ini文件这样写的:
[Connection]
SqlServerName=cnaecfuyd000072
SqlUSER=sa
SqlPWD=123623255
SqlDatabase=master



#include "gettime.h"
#include <comdef.h>
#include <atlbase.h>
#pragma warning(disable:4146)
#import "C:\Program Files\Common Files\System\ado\msado15.dll"no_namespace rename("EOF","adoEOF")
#pragma warning(default:4146)
#include <iomanip.h>
#include <string>
using namespace std;

__declspec(dllexport) LPTSTR GetTime()
{
//CString serName;
char ServerName[20];
char UserID[20];
char Password[20];
char SqlDatabase[20];


::CoInitialize(NULL);

_ConnectionPtr pConn;
pConn.CreateInstance(__uuidof(Connection));
_RecordsetPtr pRs;
pRs.CreateInstance(__uuidof(Recordset));
_CommandPtr pCommand;
pCommand.CreateInstance(__uuidof(Command));
//-------------------------Open GetSVRDT.ini to get sql parameter---------------------------

GetPrivateProfileString("Connection","SqlServerName",NULL,ServerName,20,".\\GetSVRDT.ini");
GetPrivateProfileString("Connection","SqlUSER",NULL,UserID,20,".\\GetSVRDT.ini");
GetPrivateProfileString("Connection","SqlPWD",NULL,Password,20,".\\GetSVRDT.ini");
GetPrivateProfileString("Connection","SqlDatabase",NULL,SqlDatabase,20,".\\GetSVRDT.ini");

string strConn("provider=SQLOLEDB.1;Persist Security Info=True;Initial Catalog='+SqlDatabase+';data source='+ServerName+';User ID='+UserID+';Password='+Password+'");
pConn->Open((_bstr_t)strConn.c_str(), "", "", -1);
pCommand->ActiveConnection=pConn;
pCommand->CommandText=_bstr_t("select convert(varchar(10),getdate(),110)+space(1)+convert(varchar(10),getdate(),108) as sysTime");
//pCommand->CommandText=_bstr_t("Select Convert(varchar(20),getdate(),120) AS sysTime");
pCommand->CommandType=adCmdText;
//pCommand->CommandTimeout=(long)300;
pCommand->Parameters->Refresh();
pRs=pCommand->Execute(NULL,NULL,adCmdUnknown);
_variant_t varValue=pRs->GetCollect("sysTime");
_bstr_t bstrValue=varValue.bstrVal;
LPTSTR lpBuffer = new TCHAR[100];
pRs->Close();
pConn->Close();
//lpBuffer=bstrValue;
//_stprintf(lpBuffer,_T("%s"),(LPCTSTR)bstrValue);
_stprintf(lpBuffer,_T("%s"),(LPCTSTR)bstrValue);
::CoUninitialize();
return lpBuffer;
}

怎么调用时候出错呢?帮看下哪有问题。谢谢
zhaohongbo83 2009-10-14
  • 打赏
  • 举报
回复
GetPrivateProfileString("数据库", "主机名", NULL, m_szHost, sizeof(m_szHost), IniFile) ;
GetPrivateProfileString("数据库", "用户名", NULL, m_szUser, sizeof(m_szUser), IniFile) ;
GetPrivateProfileString("数据库", "密码", NULL, m_szPwd, sizeof(m_szPwd), IniFile) ;
GetPrivateProfileString("数据库", "默认库", NULL, m_szDef, sizeof(m_szDef), IniFile) ;

try
{
//连接XdData
HRESULT hr = m_Connection.CreateInstance(__uuidof(Connection));
sprintf(m_szConnect,"provider = sqloledb;server=%s;database=%s;", m_szHost, m_szDef);
hr=m_Connection->Open(_bstr_t(m_szConnect),_bstr_t(m_szUser),_bstr_t(m_szPwd),-1);
sprintf(m_szTmp, "数据库连接成功!");
//连接XdData
}
catch(_com_error & e)
{
sprintf(m_szTmp, "数据库打开失败,错误原因:%s\n",LPCTSTR(e.Description()));
MessageBox( NULL, m_szTmp,"失败提示",MB_OK );
return 0 ;
}
参考一下,
freckyliao 2009-10-14
  • 打赏
  • 举报
回复
我用的是WIN32 dll,在里面#include <afx.h> 的时候说已经#include <windows.h>,无法编译。
freckyliao 2009-10-14
  • 打赏
  • 举报
回复
要怎么改?
我在函数里面定义CString 的时候说是 不能include <windows.h>。
去掉了以后还是这么提示,要不就说找不到<ztlstr.h>。
所以这个GetPrivateProfileString我就不知道里面参数怎么传了。
shakaqrj 2009-10-13
  • 打赏
  • 举报
回复
lpAppName
[in] Pointer to a null-terminated string that specifies the name of the section containing the key name. If this parameter is NULL, the GetPrivateProfileString function copies all section names in the file to the supplied buffer.

lpFileName
[in] Pointer to a null-terminated string that specifies the name of the initialization file. If this parameter does not contain a full path to the file, the system searches for the file in the Windows directory.

wangk 2009-10-13
  • 打赏
  • 举报
回复
你GetPrivateProfileString用法也太神奇了吧?难道连MSDN都不屑一看么?

4,017

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 数据库
社区管理员
  • 数据库
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧