MySql storedprocedure没有执行frm asp.net

时间:2014-08-04 08:35:50

标签: c# mysql asp.net stored-procedures

我能够执行MySQL sp。在服务器中它工作正常,但从asp.net调用时,它无法正常工作。以下是存储过程:

CREATE PROCEDURE `GetCategoryForBackLinkID`(IN BLID int, OUT CatID int)
BEGIN
        SELECT CategoryID INTO CatID FROM backlink where BackLinkID = BLID;
END

以下是asp.net代码

MySqlCommand cmd1 = new MySqlCommand("GetCategoryForBackLinkID");
MySqlConnection con1 = new MySqlConnection();

//ConnectionStringSettings mySetting = ConfigurationManager.ConnectionStrings["linkbuilding1Entities3"];
con1.ConnectionString = "server=67.227.183.117;User Id=rammu1;Pwd=eframmu1;database=linkbuilding1;Persist Security Info=True";
cmd1.Connection = con1;
using (cmd1.Connection)
{
     cmd1.Connection.Open();

     MySqlParameter returnParameter1 = cmd1.Parameters.Add("BLID", MySqlDbType.Int16);
     returnParameter1.Direction = ParameterDirection.Input;
     returnParameter1.Value = maximumbacklinid;

     MySqlParameter returnParameter2 = cmd1.Parameters.Add("CatID", MySqlDbType.Int16);
     returnParameter2.Direction = ParameterDirection.Output;

     cmd1.ExecuteNonQuery();                   
     CategID = (Int16)returnParameter2.Value;

我得到的错误是

  

您的SQL语法有错误;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   靠近' GetCategoryForBackLinkID'在第1行。

这里可能有什么问题?

2 个答案:

答案 0 :(得分:2)

,我不是百分百肯定,但看起来你需要帮助你的CommandType财产;

cmd1.CommandType = CommandType.StoredProcedure;

由于您使用存储过程,因此默认情况下此属性为Text。这就是为什么您的程序认为您的"GetCategoryForBackLinkID"字符串是有效的SQL查询而不是存储过程的原因。

  

当您将CommandType属性设置为StoredProcedure时,您应该这样做   将CommandText属性设置为存储过程的名称。该   当你调用其中一个时,命令执行这个存储过程   执行方法。

using(MySqlConnection con1 = new MySqlConnection(connString))
using(MySqlCommand cmd1 = con.CreateCommand())
{
   cmd1.CommandType = CommandType.StoredProcedure;
   // Add your parameter values.
   cmd1.ExecuteNonQuery();
   CategID = (int)returnParameter2.Value;
}

答案 1 :(得分:0)

IN你的连接字符串中有一个关键字是错误的写入数据源而不是数据库。

"server=67.227.183.117;User Id=rammu1;Pwd=eframmu1;Data Source=linkbuilding1;Persist Security Info=True";