本文介绍了将数据从SQL复制到mdb时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有关于mdb和sql的问题我想将数据从sql插入mdb我做了什么: 1.在mdb中使用sql table nd创建表列,类型等 2.插入每次像sql的循环记录集并插入mdb。 问题在于它的问题很多时候从sql插入mdb任何建议或更好的解决方案请让我知道。 我尝试过: for (CXInt nFld = 0; nFld<(CXInt)pmatrecordset- > NumRows(); nFld ++) { strInsertQuery = _T( ); strColNames = _T( ),strColValues = _T( ); strTemp = _T( ); strInsertQuery = _T( INSERT INTO); strInsertQuery + = strTName; strColNames = _T( (); strColNames = _T( (); strColValues = _T( VALUES(); for (CXInt nColName = 0; nColName<(CXInt)pMat-> NumCols(); nColName ++) { strColNames + = pMat-> GetColumnName(nColName); strColNames + = _T(' ,'); } strColNames = strColNames.TrimRight(' ,'); strColNames + = _T( )); for (CXInt nCol = 0; nCol<(CXInt)pMat-> NumCols(); nCol ++) { strTemp.Format(_T( \%s \ ),str); strColValues + = strTemp; strColValues + = _T(' ,'); } strColValues = strColValues.TrimRight(' ,'); strColValues + = _T( )); strInsertQuery + = strColNames; strInsertQuery + = strColValues; //将插入 mfdb } 解决方案 假设你正在使用 CString 你已经通过定义循环外的字符串并分配空字符串而不是调用 Empty()哪会释放内存。 所以 CString 对象的内存必须是仅在必要时重新分配。但是,您可以使用适当大小的 Preallocate()为所有字符串预分配内存。 其他优化将行相关部分移出行循环,并避免调用 Format()和 Trim(): // 准备查询字符串的开头对于所有行都是相同的 strQuery = _T( INSERT INTO ); strQuery + = strTName; strQuery + = _T( (); for (CXInt nCol = 0 ; nCol<(CXInt)pMat-> NumCols(); nCol ++) { if (nCol) strQuery + = _T(' ,'); strQuery + = pMat-> GetColumnName(nCol); } strQuery + = _T( )VALUES(); for (CXInt nFld = 0 ; nFld<(CXInt)pmatrecordset-> NumRows(); nFld ++) { strColValues = ; for (CXInt nCol = 0 ; nCol<(CXInt)pMat-> NumCols(); nCol ++) { if (nCol) strColValues + = _T(' ,'); strColValues + = _T(' '); // 代码中的str分配在哪里? strColValues + = str; strColValues + = _T(' '); } strColValues + = _T( )); strInsertQuery = strQuery; strInsertQuery + = strColValues; } 然后大部分执行时间应该来自数据库操作。这些也可以通过使用参数化命令进行优化,如评论中已经建议的那样。 最后,当速度很重要时,不要使用调试版本。 Debug构建添加了许多额外的代码;特别是字符串操作。 为什么要这么麻烦? MS Access可以链接到SQL Server表(以及其他内容)。 i have issue regarding mdb and sql i want to insert data from sql to mdb what i have done :1. created table in mdb with sql table nd columns , type etc2.inserting each and every time like looping recordset of sql and inserting into mdb.the problem is its taking lot of time to insert from sql to mdb any suggestion or better solution please let me known.What I have tried:for ( CXInt nFld = 0; nFld < (CXInt)pmatrecordset->NumRows(); nFld++ ){strInsertQuery = _T("");strColNames = _T(""), strColValues = _T("");strTemp = _T("");strInsertQuery = _T("INSERT INTO ");strInsertQuery += strTName;strColNames = _T("( ");strColNames = _T("( ");strColValues = _T(" VALUES (");for ( CXInt nColName = 0; nColName < (CXInt)pMat->NumCols(); nColName++ ){strColNames += pMat->GetColumnName(nColName);strColNames += _T(',');}strColNames = strColNames.TrimRight(',');strColNames += _T(" ) ");for ( CXInt nCol = 0; nCol < (CXInt)pMat->NumCols(); nCol++ ){strTemp.Format(_T("\"%s\""), str);strColValues += strTemp;strColValues += _T(',');}strColValues = strColValues.TrimRight(',');strColValues += _T(" ) ");strInsertQuery += strColNames;strInsertQuery += strColValues;//inserting to mfdb} 解决方案 Assuming you are using CString you have already done it right by defining the strings outside the loops and assigning empty strings instead of calling Empty() which would release the memory.So the memory of the CString objects must be only reallocated when necessary. However, you may pre-allocate memory for all strings using Preallocate() with appropriate sizes.Other optimisations are moving not row related parts out of the row loop, and avoiding calls to Format() and Trim():// Prepare the begin of the query string which is the same for all rowsstrQuery = _T("INSERT INTO ");strQuery += strTName;strQuery += _T(" ( ");for ( CXInt nCol = 0; nCol < (CXInt)pMat->NumCols(); nCol++ ){ if (nCol) strQuery += _T(','); strQuery += pMat->GetColumnName(nCol);}strQuery += _T(" ) VALUES ( ");for ( CXInt nFld = 0; nFld < (CXInt)pmatrecordset->NumRows(); nFld++ ){ strColValues = ""; for ( CXInt nCol = 0; nCol < (CXInt)pMat->NumCols(); nCol++ ) { if (nCol) strColValues += _T(','); strColValues += _T('"'); // Where is str assigned in your code? strColValues += str; strColValues += _T('"'); } strColValues += _T(" ) "); strInsertQuery = strQuery; strInsertQuery += strColValues;}Then most of the execution time should be from the database operations. These can be optimized too by using parametrised commands as already suggested in the comments.Finally, don't use a debug build when speed matters. Debug builds add a lot of additional code; especially to string operations.Why bother? MS Access can "link" to SQL Server tables (among other things). 这篇关于将数据从SQL复制到mdb时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 06-24 02:02