使用aspx.cs从MySQL获取返回的值/参数

时间:2019-02-11 21:29:12

标签: mysql asp.net stored-procedures

我在MySQL中有一个简单的过程(与Retrieving values of function output/proc output parameters in JavaScript from ADODB commands to MySQL?中的过程相同)。

为了解决无法获取输出参数值的死角,我从C#VisualStudio网站项目的aspx.cs组件开始进行此工作。

但是,到目前为止,该方法甚至不执行过程,更不用说返回值了。

//MySQL Procedure - where user readonly has read and execute auth
CREATE DEFINER=`root`@`localhost` PROCEDURE `TEST`(OUT V_VAR VARCHAR(10))
BEGIN
    DECLARE V_ID INT;
    SELECT MAX(ID)+1 INTO V_ID FROM storedprocandfunccalllog;
    INSERT INTO storedprocandfunccalllog VALUES(V_ID,"Test",NOW());

    SELECT 'OUCH' INTO V_VAR FROM DUAL;
    SELECT @V_VAR;
END

//Default.aspx.cs 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

using System.Data;
using MySql.Data;
using MySql.Data.MySqlClient;

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        MySqlConnection conn = new MySqlConnection();
        conn.ConnectionString = "Server=localhost; Database=mine; User=READONLY; Password=readonly; Port=3306";
        MySqlCommand cmd = new MySqlCommand();
        Response.Write("<script>alert('Here');</script>");

        conn.Open();
        cmd.Connection = conn;

        cmd.CommandText = "Call Test(@V_VAR);";
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.Clear();
        cmd.Parameters.Add(new MySqlParameter("@V_VAR", "VarChar"));
        cmd.Parameters["@V_VAR"].DbType = System.Data.DbType.String;
        cmd.Parameters["@V_VAR"].Direction = ParameterDirection.Output;
        Response.Write("<script>alert('Here');</script>");
        Response.Write("<script>console.log('Command Test: ');</script>");
        Response.Write("<script>console.log('Command Test: " + cmd.CommandText + "');console.log('Command Parameters: '); console.log('" + cmd.Parameters + "');</script>");

        try
        {
            MySqlDataReader myReader; 
            myReader = cmd.ExecuteReader();
            while (myReader.Read())
            { Console.WriteLine(myReader.GetString(0)); }
            Console.WriteLine("Return Value: " + cmd.Parameters["@V_VAR"].Value);
            HelloWorldLabel.Text = cmd.Parameters["@V_VAR"].Value.ToString();
            Response.Write("<script>console.log('Here');</script>");
        }
        catch (MySql.Data.MySqlClient.MySqlException ex)
        {
            Response.Write("<script>alert('Error' );</script>");
            Response.Write("<script>alert(\"Error: \"+"+ ex.Number + "+\" has occurred: " + ex.Message + "\");</script>");
            Response.Write("<script>console.log(\"Error: \"+" + ex.Number + "+\" has occurred: " + ex.Message + "\");</script>");
        }
        conn.Close();
        Response.Write("<script>alert('Done');</script>");
    }
}

预期结果是“ Ouch”将填充到屏幕或console.log中。

但是我得到的是:“错误:发生1414:例行mine的OUT或INOUT参数1。TEST不是触发器之前的变量或NEW伪变量”

使用CommandType.StoreProcedure交换CommandType.Text

cmd.CommandType = CommandType.StoredProcedure;

...导致:

错误:发生了0:在数据库'Call Test(@V_VAR)'中找不到过程或函数'mine'。

然后将命令文本字符串剥离为Test:

cmd.CommandText = "Test";

...结果显示错误页面:

     Data is Null. This method or property cannot be called on Null values. 
       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.Data.SqlTypes.SqlNullValueException: Data is Null. This method or property cannot be called on Null values.

     Source Error: 



     Line 45:         {
     Line 46:             MySqlDataReader myReader; 
-->  Line 47:             myReader = cmd.ExecuteReader();

**经过大量的反复试验,我已经使函数起作用了-仍然需要过程。

其他修改...数据库中的用户权限需要大于SELECT和EXECUTE ...确切的是,我不知道,但是当我切换到具有完全权限的管理员用户时,它起作用了-该功能和过程调用有效,但过程仍未返回输出值**

我有一个名为ouch的MySQL函数:

CREATE DEFINER=`root`@`localhost` FUNCTION `OUCH`() RETURNS varchar(10) CHARSET utf8mb4
    DETERMINISTIC
RETURN 'OUCH'

当我将apsx.cs文件更改为以下内容时:

    cmd.CommandText = "Ouch";
    cmd.CommandType = CommandType.StoredProcedure;

    cmd.Parameters.Clear();
    cmd.Parameters.Add(new MySqlParameter("@V_VAR", MySqlDbType.VarChar));

    cmd.Parameters["@V_VAR"].Direction = ParameterDirection.ReturnValue;
    Response.Write("<script>alert('Here');</script>");
    Response.Write("<script>console.log('Command Test: ');</script>");
    Response.Write("<script>console.log('Command Test: " + cmd.CommandText + "');console.log('Command Parameters: '); console.log('" + cmd.Parameters + "');</script>");

    try
    {
        cmd.ExecuteReader();
        HelloWorldLabel.Text = cmd.Parameters["@V_VAR"].Value.ToString();
        Response.Write("<script>console.log('" + cmd.Parameters["@V_VAR"].Value.ToString() + "');</script>");
    }
    catch (MySql.Data.MySqlClient.MySqlException ex)
    {
        Response.Write("<script>alert('Error' );</script>");
        Response.Write("<script>alert(\"Error: \"+"+ ex.Number + "+\" has occurred: " + ex.Message + "\");</script>");
        Response.Write("<script>console.log(\"Error: \"+" + ex.Number + "+\" has occurred: " + ex.Message + "\");</script>");
    }

“ OUCH”确实填充到屏幕和console.log中。所以我可以用它来返回单个值。

但是,这并不能回答为什么过程仍然不会返回输出参数的原因。

0 个答案:

没有答案