简体   繁体   English

调用存储过程后执行查询

[英]Execute a Query After Call a stored procedure

This is my stored procedure 这是我的存储过程

CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_CPC`(
    IN _B VARCHAR(100),
    IN _G      VARCHAR(2),     
    IN _R VARCHAR(30), 
    IN _D VARCHAR(30), 
    OUT _C FLOAT, 
    OUT _P FLOAT)
BEGIN
  //Something Hear
END$$
DELIMITER ;

I Call this stored procedure by C# flowing Code 我通过C#流代码调用此存储过程

DataSet tmpDataSet = new DataSet();
mCommand.CommandText = "sp_CPC";
mCommand.CommandType = CommandType.StoredProcedure;
// mCommand.CommandText = "sp_select_all_employees";
mCommand.Parameters.AddWithValue("@_B", "bty-23");
mCommand.Parameters.AddWithValue("@_G", "3");
mCommand.Parameters.AddWithValue("@_R", "9000");
mCommand.Parameters.AddWithValue("@_D", "92");

mCommand.Parameters.Add("@_C",MySqlDbType.Float);
mCommand.Parameters["@_C"].Direction = ParameterDirection.Output;
mCommand.Parameters.Add("@_P", MySqlDbType.Float);
mCommand.Parameters["@_P"].Direction = ParameterDirection.Output;

try
{
    mConnection.Open();
    mCommand.ExecuteNonQuery();
    mAdapter.Fill(tmpDataSet)
}
catch (Exception ex)
{
    strErrorInfo = ex.ToString();
}
finally
{
    mConnection.Close();
}
DataTable dtb = tmpDataSet.Tables[0];
         mCommand.CommandText = "INSERT INTO abc (xxxx,yyyy) VALUES ('" + dtb.Rows[0][0] + "','" + dtb.Rows[0][1] + "')";           
         mConnection.Open();
         mCommand.ExecuteNonQuery();     
         mConnection.Close();
return tmpDataSet;

it show error in this command mCommand.ExecuteNonQuery(); 它在此命令中显示错误mCommand.ExecuteNonQuery(); You have an error in your SQL syntax; 您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near '-14' AND name LIKE '5378377032052','6'' at line 1. 检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在'-14'附近使用,并在第1行使用类似'5378377032052','6''的名称。

(Return tmpDataSet) use Because of this data i also use anouther work (返回tmpDataSet)使用由于此数据,我还使用其他工作

The way you construct the INSERT statement is unsafe. 构造INSERT语句的方法不安全。 You concatenate strings that might contain ' characters, so that your INSERT statement becomes invalid - and also prone to SQL injection attacks. 您将可能包含'字符的字符串连接起来,从而使INSERT语句无效-并容易受到SQL注入攻击。 The error message you show in the comments points in this direction. 您在注释中显示的错误消息指向此方向。

In order to solve this, use parameters in the INSERT statement as you did in your stored procedure. 为了解决此问题,请像在存储过程中一样在INSERT语句中使用参数。

Sample: 样品:

// ...
mCommand.CommandText = "INSERT INTO abc (xxxx,yyyy) VALUES (@val1, @val2)";           
mCommand.Parameters.AddWithValue("@val1", dtb.Rows[0][0]);     
mCommand.Parameters.AddWithValue("@val2", dtb.Rows[0][1]);
// ...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM