使用C#输入更改用户声明到Oracle中使用C#
我正在制作一个工具来重置/解锁oracle用户,我通过sqlplus使用以下语句解锁用户“ALTER USER myUserName ACCOUNT UNLOCK;”但是,每次我通过我的C#代码执行语句时,我都会看到这个错误!经过数小时的试验和错误,我正在寻求帮助!任何帮助将是伟大的!使用C#输入更改用户声明到Oracle中使用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();”
回答:
请点击此链接:
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;
回答:
您永远不会将该命令与连接相关联。成规还使用了using
块,以确保命令和连接是否正确配置::
using(OracleConnection con = new OracleConnection(connectionString) using(OracleCommand cmd = new OracleCommand("ALTER USER myUserName ACCOUNT UNLOCK;", con)
{
con.Open();
cmd.ExecuteNonQuery();
}
以上是 使用C#输入更改用户声明到Oracle中使用C# 的全部内容, 来源链接: utcz.com/qa/259407.html