2016-04-15 56 views
2

我想执行一个查询,但我们面对的连接字符串的问题ASP.NET:连接被关闭

这是我的代码:

OracleCommand _commandInvoice = new OracleCommand(); 
_commandInvoice.CommandType = CommandType.StoredProcedure; 

_commandInvoice.Parameters.AddWithValue("I_INVOICE_ID", strInvoiceID); 
_commandInvoice.Parameters.AddWithValue("I_ORG_ID", ORG_ID); 
_commandInvoice.Parameters.AddWithValue("I_ORG_NAME", strOrg_name); 
_commandInvoice.Parameters.AddWithValue("I_PROJECT", strProject); 
_commandInvoice.Parameters.AddWithValue("I_VENDOR_NAME", strVendor_name); 
_commandInvoice.Parameters.AddWithValue("I_VENDOR_TYPE_LOOKUP_CODE", strVendorType_lookup_Code); 
_commandInvoice.Parameters.AddWithValue("I_INVOICE_NUMBER", strInvoice_number); 
_commandInvoice.Parameters.AddWithValue("I_INVOICE_DATE", strInvoice_date); 
_commandInvoice.Parameters.AddWithValue("I_INVOICE_AMT", strInvoice_Amt); 
_commandInvoice.Parameters.AddWithValue("I_OUTSTANDING_AMT", strOutstanding_Amt); 
_commandInvoice.Parameters.AddWithValue("I_OUTSTANDING_REQ_AMT", strOutstanding_req_amt); 

if (obj_Conn.State == ConnectionState.Closed) 
{ 
    obj_Conn.Open(); 
    _commandInvoice.ExecuteNonQuery(); 
} 

我们越来越上午错误:

Invalid operation. The connection is closed.

+2

你应该[我们已经能够停止使用AddWithValue()?]退房(http://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using- addwithvalue-already /)并停止使用'.AddWithValue()' - 它可能会导致意想不到的和令人惊讶的结果... –

+0

而且您还需要**定义**存储过程名称**,以便您的命令知道*哪个*存储过程执行!我建议在构造函数中这样做(并设置连接):'OracleCommand _commandInvoice = new OracleCommand(“storedProcedureName”,obj_Conn);' –

回答

4

您需要设置您的OracleCommand上的Connection属性以使用您的obj_Conn连接,或使用相关的constructor

_commandInvoice.Connection = obj_Conn; 
+1

或者甚至更简单:在构造函数中定义它 - 以及**存储程序名称**目前还没有设置..... –