我正在制作一个工具来重置/解锁c#中的oracle用户,我使用以下语句通过sqlplus解锁用户“ALTER USER myUserName ACCOUNT UNLOCK;”但是,每次我通过我的c#代码执行语句时,我都会收到此错误!经过数小时的试验和错误,我正在寻求帮助!任何帮助都会很棒!
另外,如果有人可以在提交声明后建议如何从Oracle获得响应?
提前谢谢大家!
public ActionResult SubmitRequest()
{
String connectionString ="Data Source = (DESCRIPTION= (ADDRESS=(PROTOCOL=TCP)(HOST=myHost)(PORT=1234)) (CONNECT_DATA=(SID=mySID)));"+
"User Id="+Username+";"+
"password="+password+ ";";
OracleConnection con = new OracleConnection();
con.ConnectionString = connectionString;
if (con.State == ConnectionState.Closed)
{
con.Open();
OracleCommand cmd = new OracleCommand();
cmd.CommandText = "ALTER USER myUserName ACCOUNT UNLOCK;"
cmd.ExecuteNonQuery();
}
con.Close();
return View();
}
错误:
Server Error in '/' Application.
Invalid operation. The connection is closed.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: Invalid operation. The connection is closed.
Source Error:
Line 134: cmd.CommandText = CommandUnlock;
Line 135: con.Open();
Line 136: cmd.ExecuteNonQuery();
我使用时也会遇到同样的错误 - “cmd.ExecuteScalar();”
答案 0 :(得分:0)
请点击此链接:
How to run 'alter user' command from C#
否则,您可以编写类似下面的过程并从C#执行它。
CREATE OR REPLACE PROCEDURE UNLOCK_USER(P_USER IN VARCHAR2) IS
BEGIN
EXECUTE IMMEDIATE 'ALTER USER ' || P_USER || ' ACCOUNT UNLOCK';
END UNLOCK_USER;
答案 1 :(得分:0)
您永远不会将命令与连接相关联。你还应该使用using
块来确保正确配置命令和连接::
using(OracleConnection con = new OracleConnection(connectionString)
using(OracleCommand cmd = new OracleCommand("ALTER USER myUserName ACCOUNT UNLOCK;", con)
{
con.Open();
cmd.ExecuteNonQuery();
}