<#@ template language="c#" HostSpecific="True" #> <#@ output extension= ".cs" #> <# TableHost host = (TableHost)(Host); host.Fieldlist.Sort(CodeCommon.CompareByintOrder); #> // 为每个字段生成注释 GO SET ANSI_PADDING OFF <# foreach (ColumnInfo c in host.Fieldlist) { #> GO EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'<#= c.ColumnName.ToString().ToLower() #>' , @level0type=N'SCHEMA', @level0name=N'dbo', @level1type=N'TABLE', @level1name=N'<#= host.GetModelClass(host.TableName) #>', @level2type=N'COLUMN', @level2name=N'<#= c.ColumnName.ToString().ToLower() #>' <# } #> /////////////////////////////////////////////////////////////////////////// // 生成查询语句 #define SELECT_<#= host.GetModelClass(host.TableName) #> _T("SELECT <# foreach (ColumnInfo c in host.Fieldlist) { #> <# if( c == host.Fieldlist[host.Fieldlist.Count-1] ) {#><#= c.ColumnName.ToString().ToLower() #><# } else { #> <#= c.ColumnName.ToString().ToLower() #>,<# } #><# } #> FROM <#= host.GetModelClass(host.TableName) #>") /////////////////////////////////////////////////////////////////////////// // 生成更新语句 #define UPDATE_<#= host.GetModelClass(host.TableName) #> _T("UPDATE [<#= host.GetModelClass(host.TableName) #>] SET \ <# foreach (ColumnInfo c in host.Fieldlist) { #> <# if( c == host.Fieldlist[host.Fieldlist.Count-1] ) {#> <#= c.ColumnName.ToString().ToLower() #>\ <# } else { #> <#= c.ColumnName.ToString().ToLower() #>,\ <# } #> <# } #> )VALUES(\ <# foreach (ColumnInfo c in host.Fieldlist) { #> <# if( c == host.Fieldlist[host.Fieldlist.Count-1] ) {#> <# if ( CodeCommon.DbTypeToCS(c.TypeName)=="int"|| CodeCommon.DbTypeToCS(c.TypeName)=="long"|| CodeCommon.DbTypeToCS(c.TypeName)=="float"|| CodeCommon.DbTypeToCS(c.TypeName)=="decimal"|| CodeCommon.DbTypeToCS(c.TypeName)=="bool") {#> %d<# } #> <# if ( CodeCommon.DbTypeToCS(c.TypeName)=="string" || CodeCommon.DbTypeToCS(c.TypeName)=="DateTime") {#> '%s' <# } #> <# } else { #> <# if ( CodeCommon.DbTypeToCS(c.TypeName)=="int"|| CodeCommon.DbTypeToCS(c.TypeName)=="long"|| CodeCommon.DbTypeToCS(c.TypeName)=="float"|| CodeCommon.DbTypeToCS(c.TypeName)=="decimal"|| CodeCommon.DbTypeToCS(c.TypeName)=="bool") {#> %d,\ <# } #> <# if ( CodeCommon.DbTypeToCS(c.TypeName)=="string" || CodeCommon.DbTypeToCS(c.TypeName)=="DateTime") {#> '%s',\ <# } #> <# } #> <# } #>)") /////////////////////////////////////////////////////////////////////////// // 生成表对应的Insert宏; #define INSERT_<#= host.GetModelClass(host.TableName) #> _T("INSERT INTO [<#= host.GetModelClass(host.TableName) #>](\ <# foreach (ColumnInfo c in host.Fieldlist) { #> <# if( c == host.Fieldlist[host.Fieldlist.Count-1] ) {#> <#= c.ColumnName.ToString().ToLower() #>\ <# } else { #> <#= c.ColumnName.ToString().ToLower() #>,\ <# } #> <# } #> )VALUES(\ <# foreach (ColumnInfo c in host.Fieldlist) { #> <# if( c == host.Fieldlist[host.Fieldlist.Count-1] ) {#> <# if ( CodeCommon.DbTypeToCS(c.TypeName)=="int"|| CodeCommon.DbTypeToCS(c.TypeName)=="long"|| CodeCommon.DbTypeToCS(c.TypeName)=="float"|| CodeCommon.DbTypeToCS(c.TypeName)=="decimal"|| CodeCommon.DbTypeToCS(c.TypeName)=="bool") {#> %d<# } #> <# if ( CodeCommon.DbTypeToCS(c.TypeName)=="string" || CodeCommon.DbTypeToCS(c.TypeName)=="DateTime") {#> '%s' <# } #> <# } else { #> <# if ( CodeCommon.DbTypeToCS(c.TypeName)=="int"|| CodeCommon.DbTypeToCS(c.TypeName)=="long"|| CodeCommon.DbTypeToCS(c.TypeName)=="float"|| CodeCommon.DbTypeToCS(c.TypeName)=="decimal"|| CodeCommon.DbTypeToCS(c.TypeName)=="bool") {#> %d,\ <# } #> <# if ( CodeCommon.DbTypeToCS(c.TypeName)=="string" || CodeCommon.DbTypeToCS(c.TypeName)=="DateTime") {#> '%s',\ <# } #> <# } #> <# } #>)") /////////////////////////////////////////////////////////////////////////// // 生成表对应的结构体; typedef struct __Tbl_<#= host.GetModelClass(host.TableName) #>__ { <# foreach (ColumnInfo c in host.Fieldlist) { #> <#= CodeCommon.DbTypeToCS(c.TypeName) #> _<#= c.ColumnName.ToString().ToLower() #>; <# } #> }Tbl<#= host.GetModelClass(host.TableName) #>,*pTbl<#= host.GetModelClass(host.TableName) #>; // 代码自动生成模板 // 生成AdoImpl类的GetTblRecordSet模板函数; /////////////////////////////////////////////////////////////////////////// /************************************************************************/ /* 函数:GetTbl_<#= host.GetModelClass(host.TableName) #>RecordSet 描述:获取表[<#= host.GetModelClass(host.TableName) #>]的记录集; 参数: IN: pTableName 表名; IN: pFilters 查找条件; IN: pSql SQL语句; IN: nFieldCount 查找的字段数量; OUT: vtRSet 返回的记录集对应的结构体数组; 返回:成功返回记录集大小, 失败返回 -1; */ /************************************************************************/ INT AdoImpl::GetTbl_<#= host.GetModelClass(host.TableName) #>RecordSet(IN CONST TCHAR *pTableName, IN CONST TCHAR *pFilters, IN CONST TCHAR *pSql, INT &nFieldCount, OUT vector>& vtRSet) { TCHAR szSQL[1024] = _T(""); if(pFilters == NULL || _tcscmp(pFilters,_T("")) == 0) _stprintf_s(szSQL,_T("select count(*) as cot from %s"),pTableName); else _stprintf_s(szSQL, _T("select count(*) as cot from %s where %s"), pTableName, pFilters); int nRecordCount = 0; _variant_t vtFieldValue; bool bResult = false; EnterCriticalSection( &m_csAdo ); bResult = GetADODateBaseStatus(); if( false == bResult ) { LeaveCriticalSection( &m_csAdo ); return -1; } CADORecordset* pRSet = new CADORecordset(m_pADODatabase); LeaveCriticalSection( &m_csAdo ); if( NULL == pRSet ) { return -1; } bResult = OpenADORecordSet(pRSet, szSQL); if( false == bResult ) { DeleteADORecordSet(pRSet); return -1; } if( !pRSet->IsFieldNull(_T("cot")) ) { pRSet->GetFieldValue(_T("cot"), nRecordCount); } else { nRecordCount = -1; } DeleteADORecordSet(pRSet); pRSet = new CADORecordset(m_pADODatabase); LeaveCriticalSection( &m_csAdo ); if( NULL == pRSet ) { return -1; } bResult = OpenADORecordSet(pRSet, pSql); if( false == bResult ) { DeleteADORecordSet(pRSet); return -1; } nFieldCount = pRSet->GetFieldCount(); for ( int i = 0; i < nRecordCount; i++) { Tbl_<#= host.GetModelClass(host.TableName) #> tagVal; <# foreach (ColumnInfo c in host.Fieldlist){ #> if( !pRSet->IsFieldNull(_T("<#= c.ColumnName.ToString() #>")) ) { pRSet->GetFieldValue(_T("<#= c.ColumnName.ToString() #>"), vtFieldValue); <# if(CodeCommon.DbTypeToCS(c.TypeName)=="int"|| CodeCommon.DbTypeToCS(c.TypeName)=="long"|| CodeCommon.DbTypeToCS(c.TypeName)=="float"|| CodeCommon.DbTypeToCS(c.TypeName)=="decimal"|| CodeCommon.DbTypeToCS(c.TypeName)=="bool") {#> tagVal._<#= c.ColumnName.ToString().ToLower() #> = vtFieldValue; <# } #> <# if(CodeCommon.DbTypeToCS(c.TypeName)=="string" || CodeCommon.DbTypeToCS(c.TypeName)=="DateTime") {#> tagVal._<#= c.ColumnName.ToString().ToLower() #> = TString((TCHAR*)(_bstr_t)vtFieldValue); <# } #> } else { <# if(CodeCommon.DbTypeToCS(c.TypeName)=="int"|| CodeCommon.DbTypeToCS(c.TypeName)=="long"|| CodeCommon.DbTypeToCS(c.TypeName)=="float"|| CodeCommon.DbTypeToCS(c.TypeName)=="bool"|| CodeCommon.DbTypeToCS(c.TypeName)=="decimal") {#> tagVal._<#= c.ColumnName.ToString().ToLower() #> = 0; <# } #> <# if(CodeCommon.DbTypeToCS(c.TypeName)=="string" || CodeCommon.DbTypeToCS(c.TypeName)=="DateTime") {#> tagVal._<#= c.ColumnName.ToString().ToLower() #> = _T(""); <# } #> } <# } #> vtRSet.push_back(tagVal); pRSet->MoveNext(); } DeleteADORecordSet(pRSet); return nRecordCount; } // 代码自动生成模板 // 生成AdoImpl类的GetTblRecordSet模板函数; /////////////////////////////////////////////////////////////////////////// /************************************************************************/ /* 函数:GetTbl_<#= host.GetModelClass(host.TableName) #>RecordSet 描述:获取表[<#= host.GetModelClass(host.TableName) #>]的记录集; 参数: IN: pTableName 表名; IN: pFilters 查找条件; IN: pSql SQL语句; IN: nFieldCount 查找的字段数量; OUT: vtRSet 返回的记录集对应的结构体数组; 返回:成功返回记录集大小, 失败返回 -1; */ /************************************************************************/ INT AdoImpl::GetTbl_<#= host.GetModelClass(host.TableName) #>RecordSet(IN CONST TCHAR *pTableName, IN CONST TCHAR *pFilters, IN CONST TCHAR *pSql, INT &nFieldCount, OUT CArray& AryRSet) { TCHAR szSQL[1024] = _T(""); if(pFilters == NULL || _tcscmp(pFilters,_T("")) == 0) _stprintf_s(szSQL,_T("select count(*) as cot from %s"),pTableName); else _stprintf_s(szSQL, _T("select count(*) as cot from %s where %s"), pTableName, pFilters); int nRecordCount = 0; _variant_t vtFieldValue; bool bResult = false; EnterCriticalSection( &m_csAdo ); bResult = GetADODateBaseStatus(); if( false == bResult ) { LeaveCriticalSection( &m_csAdo ); return -1; } CADORecordset* pRSet = new CADORecordset(m_pADODatabase); LeaveCriticalSection( &m_csAdo ); if( NULL == pRSet ) { return -1; } bResult = OpenADORecordSet(pRSet, szSQL); if( false == bResult ) { DeleteADORecordSet(pRSet); return -1; } if( !pRSet->IsFieldNull(_T("cot")) ) { pRSet->GetFieldValue(_T("cot"), nRecordCount); } else { nRecordCount = -1; } DeleteADORecordSet(pRSet); pRSet = new CADORecordset(m_pADODatabase); LeaveCriticalSection( &m_csAdo ); if( NULL == pRSet ) { return -1; } bResult = OpenADORecordSet(pRSet, pSql); if( false == bResult ) { DeleteADORecordSet(pRSet); return -1; } TCHAR buffer[65]; DWORD dwOldSize = AryRSet.GetSize(); AryRSet.SetSize(dwOldSize + nRecordCount,1); INT nIndex = dwOldSize == 0 ? 0 : dwOldSize - 1; nFieldCount = pRSet->GetFieldCount(); for ( int i = nIndex; i < nRecordCount; i++) { AryRSet.ElementAt(i).RemoveAll(); <# foreach (ColumnInfo c in host.Fieldlist){ #> if( !pRSet->IsFieldNull(_T("<#= c.ColumnName.ToString() #>")) ) { pRSet->GetFieldValue(_T("<#= c.ColumnName.ToString() #>"), vtFieldValue); <# if(CodeCommon.DbTypeToCS(c.TypeName)=="int"|| CodeCommon.DbTypeToCS(c.TypeName)=="long"|| CodeCommon.DbTypeToCS(c.TypeName)=="float"|| CodeCommon.DbTypeToCS(c.TypeName)=="decimal"|| CodeCommon.DbTypeToCS(c.TypeName)=="bool") {#> _itoa( vtFieldValue, buffer, 10 ); AryRSet.ElementAt(i).Add(buffer); <# } #> <# if(CodeCommon.DbTypeToCS(c.TypeName)=="string" || CodeCommon.DbTypeToCS(c.TypeName)=="DateTime") {#> AryRSet.ElementAt(i).Add((TCHAR*)(_bstr_t)vtFieldValue); <# } #> } else { <# if(CodeCommon.DbTypeToCS(c.TypeName)=="int"|| CodeCommon.DbTypeToCS(c.TypeName)=="long"|| CodeCommon.DbTypeToCS(c.TypeName)=="float"|| CodeCommon.DbTypeToCS(c.TypeName)=="bool"|| CodeCommon.DbTypeToCS(c.TypeName)=="decimal") {#> AryRSet.ElementAt(i).Add(_T("")); <# } #> <# if(CodeCommon.DbTypeToCS(c.TypeName)=="string" || CodeCommon.DbTypeToCS(c.TypeName)=="DateTime") {#> AryRSet.ElementAt(i).Add(_T("")); <# } #> } <# } #> pRSet->MoveNext(); } DeleteADORecordSet(pRSet); return nRecordCount; } /////////////////////////////////////////////////////////////////////////// // AdomImpl::GetTbl_<#= host.GetModelClass(host.TableName) #>RecordSet()的调用实例; // void Call_GetTbl_<#= host.GetModelClass(host.TableName) #>RecordSet_Example() { // 获取配置文件信息; if ( -1 == GetIniInfo() ) { // 打开配置文件失败; return; } STAdoDatabaseInfo tagAdoDatabaseInfo; _stprintf_s(tagAdoDatabaseInfo.szDataSource,_T("%s"),g_szDBSource); _stprintf_s(tagAdoDatabaseInfo.szDatabaseTCPPort,_T("%d"),g_dwDBServerPort); _stprintf_s(tagAdoDatabaseInfo.szDatabaseAccount,_T("%s"),g_szDBAccount); _stprintf_s(tagAdoDatabaseInfo.szDatabasePassword,_T("%s"),g_szDBPassWord); _stprintf_s(tagAdoDatabaseInfo.szDatabaseName,_T("%s"),g_szDBName); // 调用AdoImpl实例; AdoImpl tagAdoImpl; tagAdoImpl.SetConnectString(ADO_SQLSERVER, tagAdoDatabaseInfo); if ( tagAdoImpl.opendatabase() ) { // 打开数据库失败; return; } // 返回结果; INT nRetVal = 0; // 字段数量; INT nFieldCount = 0; // 返回的记录集结构体数组; vector> vtRSet; TCHAR szSQL[MAX_PATH] = {0}; TCHAR szFilters[MAX_PATH] = {0}; nRetVal = tagAdoImpl.GetTbl_<#= host.GetModelClass(host.TableName) #>RecordSet( _T("<#= host.GetModelClass(host.TableName) #>"), szFilters, szSQL, nFieldCount, vtRSet); if ( nRetVal == -1 ) { // 获取表数据集失败; return; } // 成功获取表数据集; // do something; // 关闭数据库连接; tagAdoImpl.closedatabase(); } /////////////////////////////////////////////////////////////////////////// // 对表[<#= host.GetModelClass(host.TableName) #>]使用宏 INSERT_<#= host.GetModelClass(host.TableName) #> 插入数据; // 将vtValues的数据写入数据库表; void Call_INSERT_<#= host.GetModelClass(host.TableName) #>_MacroExample(IN vector> &vtValues) { // 获取配置文件信息; if ( -1 == GetIniInfo() ) { // 打开配置文件失败; return; } STAdoDatabaseInfo tagAdoDatabaseInfo; _stprintf_s(tagAdoDatabaseInfo.szDataSource,_T("%s"),g_szDBSource); _stprintf_s(tagAdoDatabaseInfo.szDatabaseTCPPort,_T("%d"),g_dwDBServerPort); _stprintf_s(tagAdoDatabaseInfo.szDatabaseAccount,_T("%s"),g_szDBAccount); _stprintf_s(tagAdoDatabaseInfo.szDatabasePassword,_T("%s"),g_szDBPassWord); _stprintf_s(tagAdoDatabaseInfo.szDatabaseName,_T("%s"),g_szDBName); // 调用AdoImpl实例; AdoImpl tagAdoImpl; tagAdoImpl.SetConnectString(ADO_SQLSERVER, tagAdoDatabaseInfo); if ( tagAdoImpl.opendatabase() ) { // 打开数据库失败; return; } CString strInsert = _T(""); for ( vector>::iterator it = vtValues.begain(); it != vtValues.end(); it++ ) { strInsert.Format(INSERT_<#= host.GetModelClass(host.TableName) #>, <# foreach ( ColumnInfo c in host.Fieldlist) { #> <# if ( c == host.Fieldlist[host.Fieldlist.Count - 1] ) { #> <# if ( CodeCommon.DbTypeToCS(c.TypeName)=="int"|| CodeCommon.DbTypeToCS(c.TypeName)=="long"|| CodeCommon.DbTypeToCS(c.TypeName)=="float"|| CodeCommon.DbTypeToCS(c.TypeName)=="decimal"|| CodeCommon.DbTypeToCS(c.TypeName)=="bool") { #> it->_<#= c.ColumnName.ToString().ToLower() #> <# } #> <# if ( CodeCommon.DbTypeToCS(c.TypeName)=="string" || CodeCommon.DbTypeToCS(c.TypeName)=="DateTime") {#> it->_<#= c.ColumnName.ToString().ToLower() #>.c_str() <# } #> <# } else { #><# if ( CodeCommon.DbTypeToCS(c.TypeName)=="int"|| CodeCommon.DbTypeToCS(c.TypeName)=="long"|| CodeCommon.DbTypeToCS(c.TypeName)=="float"|| CodeCommon.DbTypeToCS(c.TypeName)=="decimal"|| CodeCommon.DbTypeToCS(c.TypeName)=="bool") { #> it->_<#= c.ColumnName.ToString().ToLower() #>,<# } #> <# if ( CodeCommon.DbTypeToCS(c.TypeName)=="string" || CodeCommon.DbTypeToCS(c.TypeName)=="DateTime") {#> it->_<#= c.ColumnName.ToString().ToLower() #>.c_str(),<# } #><# } #><# } #>); if ( !tagAdoImpl.Execute(strInsert) ) { // 执行SQL语句失败; continue; } } // 关闭数据库; tagAdoImpl.closedatabase(); }