2010-09-03 20 views
1

我是新來的SQL CE。我是在傳統的ASP編程,使用ADODB.Connection連接SQL CE。我已經創建了表並嘗試從ASP插入數據。我嘗試了3種方法。經典ASP ADODB與SQL Server Compact 3.5版中沒有指揮工作參數

  1. 直列插入語句[例如INSERT INTO tblName(col1,col2)VALUES(1,2)](WORKED)
  2. 參數化插入語句[例如, INSERT INTO tblName(Col1中)VALUES(?)](工程)。我添加了命令參數並提供了該值。
  3. 多個PARAM(失敗)參數插入語句

我不知道自己做錯了什麼有多個參數。它的Cmd.Execute語句運行時扔我未處理的錯誤。

「遠程過程調用失敗並且未執行。」

我做了很多Google來找出問題。但沒用。我沒有得到任何線索。

請幫我解決這個問題

-Ganesh

回答

0

你設置的命令對象爲adCmdText的CommandType屬性?

cmd.CommandType = adCmdText 

這就是我如何做到這一點:

dim sql : sql = "insert into tbl(fld1, fld2) values(?, ?)" 
dim cmd : set cmd = server.createObject("ADODB.Command") 
cmd.ActiveConnection = adodbConnection 
cmd.CommandType = adCmdText 

set param = cmd.CreateParameter("fld1", adVarWChar, , 20, "value1") 
cmd.Parameters.Append param 
set param = cmd.CreateParameter("fld2", adVarWChar, , 20, "value2") 
cmd.Parameters.Append param 

cmd.CommandText = sql 
cmd.Execute 
0

我想補充這個問題,我自己的示例代碼,這也失敗,所以我也正在尋找相同的答案。請注意,這個示例是用C++編寫的,所以,雖然我不能直接對ulluoink發佈的答案提出異議,但VB答案不適用於我和(我懷疑)OP。

// AdoCe.cpp : Defines the entry point for the console application. 
// 

#include "stdafx.h" 
#include <windows.h> 

#include <ole2.h> 
#import "c:\Program Files\Common Files\System\ADO\msado15.dll" no_namespace rename("EOF", "EndOfFile") 

#include <string> 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    ::CoInitializeEx(NULL, COINIT_MULTITHREADED); 

    _ConnectionPtr pConnection = NULL; 
    _CommandPtr pCommand = NULL; 
    std::string dbConnStr, sql; 

    dbConnStr = "Provider=Microsoft.SQLSERVER.CE.OLEDB.3.5;Data Source=F:\\sample.sdf;Persist Security Info=False;"; 

    // this sql insert works 
    // sql = "INSERT INTO foo (c1) VALUES (?)"; 
    // this sql insert fails 
    sql = "INSERT INTO foo (c1,c2) VALUES (?,?)"; 

    try { 
     pConnection.CreateInstance(__uuidof(Connection)); 
     HRESULT hr = pConnection->Open(dbConnStr.c_str(), "", "", adConnectUnspecified); 

     pCommand.CreateInstance(__uuidof(Command)); 
     pCommand->ActiveConnection = _ConnectionPtr(pConnection); 
     pCommand->CommandType = adCmdText; 
     pCommand->CommandText = sql.c_str(); 

     _ParameterPtr p1 = NULL; 
     // !!! assume that our parameters can always be represented as strings 
     // !!! also, CE requires adVarWChar or it generates unknown data type error 
     p1 = pCommand->CreateParameter("@p1", adVarWChar, adParamInput, 100, "1"); 
     pCommand->Parameters->Append(p1); 


     _ParameterPtr p2 = NULL, p3 = NULL, p4 = NULL, p5 = NULL; 
     p2 = pCommand->CreateParameter("@p2", adVarWChar, adParamInput, 100, "cc1"); 
     pCommand->Parameters->Append(p2); 
     //p3 = pCommand->CreateParameter("@p3", adVarWChar, adParamInput, 128, "cc2"); 
     //pCommand->Parameters->Append(p3); 
     //p4 = pCommand->CreateParameter("@p4", adVarWChar, adParamInput, 128, "cc3"); 
     //pCommand->Parameters->Append(p4); 
     //p5 = pCommand->CreateParameter("@p5", adVarWChar, adParamInput, 128, "cc4"); 
     //pCommand->Parameters->Append(p5); 

     pCommand->Execute(NULL, NULL, adExecuteNoRecords); 

     pConnection->Close(); 
    } 

    catch(_com_error& e) 
    { 
     _bstr_t e1 = e.Description(); 
     const TCHAR* e2 = e.ErrorMessage(); 
     IErrorInfo* e3 = e.ErrorInfo(); 
    } 

    ::CoUninitialize(); 
    return 0; 
} 

我希望有人能提供一個解決方案,或明確確認,這根本不會因爲一些ADO/CE限制下進行工作。