简体   繁体   English

无法将“System.String”类型的 object 转换为在 C# 中键入“Oracle.DataAccess.Client.OracleParameter”

[英]Unable to cast object of type 'System.String' to type 'Oracle.DataAccess.Client.OracleParameter' in C#

I am trying to call a stored procedure in a C# project that returns 2 values in addition to some input parameters in Oracle 11g.我正在尝试调用 C# 项目中的存储过程,该项目除了返回 Oracle 11g 中的一些输入参数外还返回 2 个值。 The procedure is a simple login that returns a varchar2 ('T' or 'F') ( oracle does not accept booleans? ) if there is a user stored that matches the username and password, and it should return a number (1 or 2) for the type of user that it is.该过程是一个简单的登录,它返回一个 varchar2('T' 或 'F')( oracle 不接受布尔值? )如果存储了一个与用户名和密码匹配的用户,它应该返回一个数字(1 或 2 ) 的用户类型。

If I test the procedure with just the ' pexito '(success) output parameter it works, but it doesn't with the ' ptipo '(type of user)) parameter.如果我使用“ pexito ”(成功)output 参数测试该过程,它可以正常工作,但不能使用“ ptipo ”(用户类型))参数。

I tried changing the datatype of the 'ptipo' parameter to varchar2 and string in the c# code, parsing in different ways, converting to string and then to int and lots of other things.我尝试在 c# 代码中将“ptipo”参数的数据类型更改为 varchar2 和字符串,以不同的方式解析,转换为字符串,然后转换为 int 和许多其他内容。 Nothing works, same error always.没有任何效果,总是出现同样的错误。

"Unable to cast object of type 'System.String' to type 'Oracle.DataAccess.Client.OracleParameter'" “无法将类型为‘System.String’的 object 转换为类型‘Oracle.DataAccess.Client.OracleParameter’”

Here is the PL/SQL SP code:这是 PL/SQL SP 代码:

    CREATE OR REPLACE PROCEDURE sp_login
(puser IN VARCHAR2, ppass IN VARCHAR2,  pexito OUT VARCHAR2, ptipo OUT NUMBER )
AS
lfila NUMBER;
ltipo number;
BEGIN

SELECT COUNT(*)
INTO lfila
FROM usuario
WHERE user_login = puser AND pass_login = ppass;

SELECT idtipo_user
INTO ltipo
FROM usuario
WHERE user_login = puser AND pass_login = ppass;

IF lfila = 0 THEN pexito:='F';
ELSE pexito:='T';
END IF;

IF ltipo =NULL THEN ptipo:=NULL;
ELSE ptipo:=ltipo;
END IF;


END;
/

And here is the C# code:这是 C# 代码:

using System;
using System.Data;
using System.Windows.Forms;
using Oracle.DataAccess.Client;
//using System.Data.OracleClient; DEPRECATED
using Sistema_On_Tour.Vistas;
using Sistema_On_Tour.Controlador;

    private void BtnIniciar_Click(object sender, EventArgs e)
            {
                OracleConnection conn = new OracleConnection(Conexion.conn);

                try
                {
                    conn.Open();
                    OracleCommand cmd = new OracleCommand("sp_login", conn);
                    cmd.CommandType = CommandType.StoredProcedure;

                    OracleParameter paruser = new OracleParameter("puser", OracleDbType.Varchar2);
                    paruser.Value= TxtUser.Text;
                    paruser.Direction = ParameterDirection.Input;
                    cmd.Parameters.Add(paruser);


                    OracleParameter parpass = new OracleParameter("ppass", OracleDbType.Varchar2);
                    parpass.Value = TxtPass.Text;
                    parpass.Direction = ParameterDirection.Input;
                    cmd.Parameters.Add(parpass);

                    OracleParameter parexito = new OracleParameter("pexito", OracleDbType.Varchar2);
                    parexito.Direction = ParameterDirection.Output;
                    parexito.Size = 1; 
                    cmd.Parameters.Add(parexito);

                    OracleParameter ptipo = new OracleParameter("ptipo", OracleDbType.Int32);
                    ptipo.Direction = ParameterDirection.Output;
                    ptipo.Size = 1; 
                    cmd.Parameters.Add("ptipo");

                    cmd.ExecuteNonQuery();
                    string exito = cmd.Parameters["pexito"].Value.ToString();
                    int tipouser = int.Parse(cmd.Parameters["ptipo"].Value.ToString());

                    if (exito.Equals('T'))
                    {
                        if (tipouser == 1)
                        {
                            this.Hide();
                            VentanaPrincipalApoderado v = new VentanaPrincipalApoderado();
                            v.Show();
                        }
                        else if(tipouser==2)
                        {
                            this.Hide();
                            VentanaPrincipalEjecutivo v = new VentanaPrincipalEjecutivo();
                            v.Show();
                        }
                    }


                    MessageBox.Show(exito);
                }
                catch(Exception error)
                {
                    MessageBox.Show(error.Message);

                }
                finally
                {
                    conn.Close();
                }

            }
        }
    } 

There is a problem with Line 线路有问题

cmd.Parameters.Add("ptipo");

Instead you have to do 相反,你必须做

   cmd.Parameters.Add(ptipo);

Note : You have to pass Variable not name of the variable as a string. 注意:您必须将变量而不是变量名作为字符串传递。

This Kind of thing will not work这种事情行不通

OracleParameter paruser = new OracleParameter("puser", OracleDbType.Varchar2);
                    paruser.Value= TxtUser.Text;
                    paruser.Direction = ParameterDirection.Input;
                    cmd.Parameters.Add(paruser);    

Do just like below code other wise you will get conversion error.就像下面的代码一样,否则你会得到转换错误。

cmd.Parameters.Add("P_UserName", OracleDbType.Varchar2).Value = login.UserName;
cmd.Parameters.Add("P_UPassword", OracleDbType.Varchar2).Value = login.Password;
cmd.Parameters.Add("cur_splogin", OracleDbType.RefCursor).Direction = ParameterDirection.Output;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将类型为“ System.Int32”的对象转换为类型为“ Oracle.DataAccess.Client.OracleParameter” - Unable to cast object of type 'System.Int32' to type 'Oracle.DataAccess.Client.OracleParameter' C# 中的 MS Word 自动化 - 无法将类型为“System.String[*]”的 object 转换为类型“System.String[]” - MS Word Automation in C# - Unable to cast object of type 'System.String[*]' to type 'System.String[]' C# 无法将“system.string”类型的对象转换为“system.int32”类型 - C# unable to cast object of type 'system.string' to type 'system.int32' 无法将“System.DateTime”类型的对象转换为“System.String”类型。 C# - Unable to cast object of type 'System.DateTime' to type 'System.String'. c# 无法将类型“ System.String”强制转换为类型“ System.Object” - Unable to cast the type 'System.String' to type 'System.Object' 无法将类型为“ System.String”的对象转换为类型为“ System.Array”的Oracle批量插入 - Unable to cast object of type 'System.String' to type 'System.Array' Oracle bulk insert 无法将类型类的对象强制转换为“ System.String”类型 - Unable to cast object of type class to type 'System.String' 无法将“System.String”类型的对象转换为“System.Collections.Generic.List`1[System.String]”类型 - Unable to cast object of type 'System.String' to type 'System.Collections.Generic.List`1[System.String]' 无法将“DapperRow”类型的对象转换为“System.String”类型 - Unable to cast object of type 'DapperRow' to type 'System.String' 无法将“System.string”类型的对象转换为“....Event”类型 - Unable to cast object of type 'System.string' to type '....Event'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM