VC调用SQL Server存储过程的通用方法的相关知识是本文我们主要要介绍的内容,这里讨论的是ADO调用存储过程,按MSDN的例子,一般都是下面的调用方法(节选自MSDN):
- HRESULT hr = S_OK;
- // Define ADO object pointers.
- // Initialize pointers on define.
- // These are in the ADODB:: namespace.
- _RecordsetPtr pRstByRoyalty = NULL;
- _RecordsetPtr pRstAuthors = NULL;
- _CommandPtr pcmdByRoyalty = NULL;
- _ParameterPtr pprmByRoyalty = NULL;
- _ConnectionPtr pConnection = NULL;
- _bstr_t strCnn("Provider=sqloledb;Data Source=srv;"
- "Initial Catalog=Pubs;User Id=sa;Password=;");
- _bstr_t strMessage, strAuthorID;
- int intRoyalty;
- VARIANT vtRoyalty;
- try
- {
- //Open a Connection.
- TESTHR(pConnection.CreateInstance(__uuidof(Connection)));
- hr = pConnection->Open(strCnn,"","",NULL);
- pConnection->CursorLocation = adUseClient;
- //Open Command Object with one Parameter
- TESTHR(pcmdByRoyalty.CreateInstance(__uuidof(Command)));
- pcmdByRoyalty->CommandText = "byroyalty";
- pcmdByRoyalty->CommandType = adCmdStoredProc;
- //Define Integer/variant.
- vtRoyalty.vt = VT_I2;
- vtRoyalty.iVal = intRoyalty;
- pprmByRoyalty = pcmdByRoyalty->CreateParameter(
- "percentage",adInteger,adParamInput,sizeof(int),vtRoyalty);
- pcmdByRoyalty->Parameters->Append(pprmByRoyalty);
- pprmByRoyalty->Value = vtRoyalty;
- //Create Recordset by executing the command
- pcmdByRoyalty->ActiveConnection = pConnection;
- pRstByRoyalty = pcmdByRoyalty->Execute(
- NULL,NULL,adCmdStoredProc);
- //执行结果的处理,省略...
- pRstByRoyalty->Close();
- pRstAuthors->Close();
- pConnection->Close();
- }
- catch(_com_error &e)
- {
- //意外处理...
- }
- }
必须指定执行SQL语句的CommandType是adCmdStoredProc,存储过程有参数就必须CreateParameter生成参数,这样调用存储过程就成了很麻烦的一件事情,必须针对不同的存储过程生成不同的参数。一个两个还可以接受,如果项目的业务逻辑比较复杂,需要依赖大量的存储过程,并且项目的需求或功能可能要经常变动或扩展,那就成了一个噩梦了。有没有通用的方法呢?当然有,只需要通用的查询就可以实现。
- _ConnectionPtr pConnection = NULL;
- m_Conn.CreateInstance(__uuidof(Connection));
- m_Conn->ConnectionString = "Provider=sqloledb;Data Source=srv;"Initial Catalog=Pubs;User Id=sa;Password=;";
- m_Conn->Open("","","",NULL);
- _variant_t m_param;
- _bstr_t m_bstr;
- int index = 0,nFieldCount;
- _RecordsetPtr m_Rs;
- m_Rs.CreateInstance(__uuidof(Recordset));
- m_Rs->Open("select * from titles",(IDispatch*)(m_Conn->m_Conn),adOpenStatic,adLockReadOnly,adCmdText);
- nFieldCount = m_Rs->Fields->Count;
- while(!m_Rs->EndOfFile)
- {
- for(index=0;index < nFieldCount;index ++)
- {
- m_param.Clear();
- m_param = (short)index;
- m_param = m_Rs->Fields->GetItem(m_param);
- if(m_param.vt != VT_NULL)
- m_bstr = m_param;
- else
- m_bstr = "";
- printf("%s/n",(char*)m_bstr);
- }
- m_Rs->MoveNext();
- }
- m_Rs->Close();
- m_Conn->Close();
以上一段就是普通的执行查询SQL语句的代码,简单起见,去掉了所有防护和判断代码,对于MS SQLServer,如果是执行返回结果集的存储过程,比如Pubs的byroyalty,只需要把上面代码中的"select * from titles"替换成"byroyalty 100"就可以了,即"过程名 参数1,参数2,..."形式。
这样一来,不需要生成参数,只需要改变SQL语句,就实现了MS SQLServer存储过程的调用,可以通用。但是对于返回参数的存储过程,这段代码就不行了,不过还是可以通过SQL语句解决:“declare @q int exec checkpwd 'user','pwd',@ret = @q OUTPUT select @q”checkpwd是一个存储过程,两个输入参数,用户名和密码,第三个是输出参数@ret,返回用户验证的结果。
通过执行上面的SQL语句,把输出参数作为结果集返回,就可以适应上面的VC代码了。返回值的存储过程也是一样,执行“declare @q int,@return intexec @return = checkpwd 'user','pwd',@ret = @q OUTPUT select @q,@return”就可以了。
以上就是VC调用SQL Server存储过程的通用方法的相关知识,本文我们就介绍到这里了,希望本次的介绍能够对您有所收获!
【编辑推荐】