Access SqlServer 获取主键

Access SqlServer 获取主键,第1张

概述#include <afxdao.h>#include <icrsint.h>#include <basetsd.h>#include <ole2.h>int CDBManager::GetPrimaryKey(CString path, CString dbsource, DATEBASETYPE dbtype,CString ServerStr, CString TableName,
#include <afxdao.h>#include <icrsint.h>#include <basetsd.h>#include <ole2.h>int CDBManager::GetPrimaryKey(CString path,CString dbsource,DATEBASETYPE dbtype,CString ServerStr,CString tablename,vector<CString>& List){	_bstr_t bstrConn;	if(dbtype == ACCESS)		bstrConn = "ProvIDer='Microsoft.JET.olEDB.4.0';Data source="+path;	else if(dbtype == sqlSERVER)	{		CString strMdbConn;		strMdbConn.Format("ProvIDer='sqlNCli.1';Integrated Security=sspI;Persist Security Info=False;Initial Catalog=%s;Data Source=%s",dbsource,ServerStr);		bstrConn = strMdbConn;	}	else	{		return 0;	}	int ok = 1;	SAFEARRAY   FAR*   psa   =   NulL; 	SAFEARRAYBOUND   rgsabound; 	_variant_t     var; 	_variant_t     Array; 	rgsabound.lLbound   =   0; 	rgsabound.cElements   =   3; 	psa   =   SafeArrayCreate(VT_VARIANT,1,&rgsabound); 	var.vt   =   VT_EMPTY; 	long   ix; 	//PK_table_CATALOG 	ix   =   0; 	SafeArrayPutElement(psa,&ix,&var); 	//PK_table_SCHEMA 	ix=1; 	SafeArrayPutElement(psa,&var); 	//PK_table_name 	ix=2; 	var.vt=VT_BSTR; 	var.bstrVal=_bstr_t(tablename);//表名 	SafeArrayPutElement(psa,&var); 	Array.vt   =   VT_ARRAY|VT_VARIANT; 	Array.parray   =   psa;  	IADORecordBinding   *picRs = NulL;	_RecordsetPtr pRstSchema("ADODB.Recordset");	_ConnectionPtr pConnection("ADODB.Connection");	pConnection->ConnectionString = bstrConn;	if(dbtype==ACCESS)		pConnection->ProvIDer = "Microsoft.Jet.olEDB.4.0";	else if(dbtype == sqlSERVER)		pConnection->ProvIDer = "sql Server";	try	{		CString strMsg,strMessage;		pConnection->Open(pConnection->ConnectionString,"",adModeUnkNown);		pRstSchema->queryInterface(__uuIDof(IADORecordBinding),(LPVOID*)&picRs);		pRstSchema = pConnection->OpenSchema(adSchemaPrimaryKeys,&Array);				int ncnt = pRstSchema->GetFIElds()->Count;		for(int i = 0; i < ncnt; i++)		{			_bstr_t   value=pRstSchema->GetFIElds()->GetItem((long)i)->name;			strMessage.Format("%s,",(LPCTSTR)value);			strMsg += strMessage;		}		while(!(pRstSchema->adoEOF))		{			CString strtableType;			_bstr_t   column_name=pRstSchema->FIElds->GetItem("ColUMN_name")->Value; 			_bstr_t   primarykey_name=pRstSchema->FIElds->GetItem("PK_name")->Value; 			CString Strtablename(pRstSchema->FIElds->GetItem("table_name")->Value);			if(Strtablename == tablename)				List.push_back((LPCTSTR)column_name);			//strMsg.Format( "column_name:%s\nprimary key name:%s ",(LPCTSTR)column_name,(LPCTSTR)primarykey_name); 			pRstSchema->MoveNext();		}		SafeArrayDestroy(psa);		// Clean up objects before exit.		pRstSchema->Close();		pConnection->Close();	}	catch (_com_error &e)	{		CString *ptr = new CString((LPCSTR)e.Description());		PostMessage(m_pDlg->GetSafeHwnd(),TIPINFO,(WParaM)ptr,0);		ok = 0;	}	return ok;}
// 使用前需初始化com
总结

以上是内存溢出为你收集整理的Access/ SqlServer 获取主键全部内容,希望文章能够帮你解决Access/ SqlServer 获取主键所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1167698.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-01
下一篇 2022-06-01

发表评论

登录后才能评论

评论列表(0条)

保存