在任意目录新建一个.udl文件,双击打开“数据链接属性”对话框,设置完成并单击[确定]之后,打开此.udl文件,即可得连接字符串。如果是Access等文件型数据库,.udl文件会被存储到数据库文件目录下。

可以使用如下批处理自动建立.udl文件并打开“数据链接属性”对话框:
@echo off
fsutil file createnew ConnectionString.udl 0 > NUL
Rundll32.exe "C:\Program Files\Common Files\System\OLE DB\oledb32.dll",
    OpenDSLFile ConnectionString.udl

也可以使用C++编程调用“数据链接属性”对话框:

#import "C:\Program Files\Common Files\System\ado\msado15.dll" rename("EOF", "rsEOF")
#import "C:\Program Files\Common Files\System\OLE DB\oledb32.dll"

int test()
{
	CoInitialize(NULL);

	//_COM_SMARTPTR_TYPEDEF(IDataSourceLocator, __uuidof(IDataSourceLocator));
	//typedef _com_ptr_t<_com_IIID<IDataSourceLocator, 
        //       &__uuidof(IDataSourceLocator)> > IDataSourceLocatorPtr;
	MSDASC::IDataSourceLocatorPtr ptrDataSourceLocator;
	HRESULT hr = ptrDataSourceLocator.CreateInstance(__uuidof(MSDASC::DataLinks));
	if (FAILED(hr))
	{
		printf("Create IDataSourceLocator failure:0x%08X\n",hr);
		return -1;
	}

	//_COM_SMARTPTR_TYPEDEF(_Connection, __uuidof(_Connection));
	//typedef _com_ptr_t<_com_IIID<_Connection, 
        //       &__uuidof(_Connection)> > _ConnectionPtr;
	ADODB::_ConnectionPtr ptrConn=ptrDataSourceLocator->PromptNew();	
	if (ptrConn!=NULL)
	{
		_bstr_t bstr_ConnectionString=ptrConn->ConnectionString;
		char* lpConnectionString=bstr_ConnectionString;

		//得到连接字符串
		cout<<lpConnectionString<<endl;

		ptrConn.Release();
		ptrConn=NULL;
	}

	ptrDataSourceLocator.Release();
	ptrDataSourceLocator=NULL;

	CoUninitialize();
	return 0;
}

关于ADO的一点问题
在Win7 SP1下编译的程序,放在XP,2003等系统上无法运行,提示“无效指针”的解决方法1:

#import "C:\Program Files\Common Files\System\ado\msado15.dll"
//修改为
#import "C:\Program Files\Common Files\System\ado\msado28.tlb"

参考:An ADO-based application that is compiled in Windows 7 SP1 or in Windows Server 2008 R2 SP1 does not run in earlier versions of Windows

方法2:
直接复制XP的msado15.dll过来。


本文链接地址: 自动生成ADO连接字符串的方法
https://blog.qingfengju.com/index.asp?id=320

分类:Win32/C++ 查看次数:12204 发布时间:2013/2/5 14:17:44

static void Main(String[] args)
{
	if (!Directory.Exists(Application.StartupPath + "\\Log"))
	{
		Directory.CreateDirectory(Application.StartupPath + "\\Log");
	}
	
	//全局异常处理
	Application.ThreadException+=
		new ThreadExceptionEventHandler(Application_ThreadException);
	
	Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
	AppDomain.CurrentDomain.UnhandledException+=
		new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
        //...
}

static void ProcessCrash(Exception e) { String path = Application.StartupPath + "\\Log\\test1.exe_" + DateTime.Now.ToString("yyyy_MM_dd_HHmmss") + ".log"; StreamWriter sw = File.CreateText(path); sw.WriteLine("test1.exe崩溃日志:"); sw.WriteLine("-----------------------------------------------"); sw.WriteLine("[ Error: ]" + e.GetType().ToString()); sw.WriteLine("[ Message: ]" + e.Message); sw.WriteLine("[ Source: ]" + e.Source); sw.WriteLine("[ StackTrace: ]----------------------------\r\n" + e.StackTrace); sw.Flush(); sw.Close(); Application.Restart(); Environment.Exit(0); } static void Application_ThreadException(object sender, ThreadExceptionEventArgs e) { Exception exp = e.Exception; MainClass.ProcessCrash(exp); } static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { Exception exp = (Exception)e.ExceptionObject; MainClass.ProcessCrash(exp); }

本文链接地址: .Net程序的崩溃日志与自动重启
https://blog.qingfengju.com/index.asp?id=317

分类:Win32/C++ 查看次数:6189 发布时间:2013/2/3 22:14:25