C#错误,执行存储过程,存储过程不可用

时间:2013-04-19 11:07:45

标签: c# mysql stored-procedures

我有一个问题,我在我的MySQL数据库中存储过程名为:sp_insertSupplier。在mysql数据库中执行时,此方法正常工作。

我在“保存”按钮

中创建此语法
private void btn_supplier_save_Click(object sender, EventArgs e)
{
    string connections = Connection.mysqlconnectionbuilder();
    using (MySqlConnection conn = new MySqlConnection(connections))
    {
        using (MySqlCommand cmd = new MySqlCommand()) 
        {
            //make connection
            cmd.Connection = conn;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "call sp_insertSupplier(@KD,@NM,@AL,@EM,@TLP);";
            cmd.Parameters.AddWithValue("@KD", createCode()); 
            cmd.Parameters.AddWithValue("@NM", txtsuppliernama.Text); 
            cmd.Parameters.AddWithValue("@AL", txtsupplieralamat.Text); 
            cmd.Parameters.AddWithValue("@EM", txtsupplieremail.Text); 
            cmd.Parameters.AddWithValue("@TLP",txtsuppliernotelp.Text);
            try
            {
                conn.Open();
                int result = cmd.ExecuteNonQuery();
                //check the result status
                if (result.Equals(1))
                {
                    DialogResult results = MessageBox.Show("Data berhasil ditambahkan! \n Apakah anda ingin input data lainnya? ", "Sukses!", MessageBoxButtons.OKCancel);
                    //cek apabila ingin menambahkan data lagi.
                    if (results.Equals(DialogResult.OK))
                    {
                        reset();
                    }
                    else if (results.Equals(DialogResult.Cancel))
                    {
                        this.Close();
                    }
                }
            } //end TRY
            catch (Exception exe)
                {
                    MessageBox.Show("Terjadi Kesalahan", "Perhatian!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    StreamWriter _writer = new StreamWriter("E899911823.log");
                    _writer.WriteLine(exe.ToString());
                    Console.WriteLine(exe.ToString());
                } //END CATCH
            finally 
                {
                    conn.Clone();
                } //END FINALLY
        }
    }
}

但是,当我执行语法时,它显示错误:

MySql.Data.MySqlClient.MySqlException (0x80004005): Procedure or function '`call sp_insertSupplier(@KD,@NM,@AL,@EM,@TLP)`' cannot be found in database XXXXX

我认为连接管理器或创建@KD的随机方法没有任何问题。

我还有同样的问题,控制台显示:

MySql.Data.MySqlClient.MySqlException (0x80004005): Procedure or function '`call sp_insertSupplier`' cannot be found in database '`XXXXX`'.
at MySql.Data.MySqlClient.ProcedureCache.GetProcData(MySqlConnection connection, String spName)
at MySql.Data.MySqlClient.ProcedureCache.AddNew(MySqlConnection connection, String spName)
at MySql.Data.MySqlClient.ProcedureCache.GetProcedure(MySqlConnection conn, String spName, String cacheKey)
at MySql.Data.MySqlClient.StoredProcedure.GetParameters(String procName, DataTable& proceduresTable, DataTable& parametersTable)
at MySql.Data.MySqlClient.StoredProcedure.CheckParameters(String spName)
at MySql.Data.MySqlClient.StoredProcedure.Resolve(Boolean preparing)
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()
at SerbaManisInventory.SupplierAdd.btn_supplier_save_Click(Object sender, EventArgs e)

我只是重写代码,以便代码如下:

using (MySqlCommand cmd = new MySqlCommand()) 
{
    cmd.Connection = conn;
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "call sp_insertSupplier";
    cmd.Parameters.AddWithValue("@KD", createCode()); 
    cmd.Parameters.AddWithValue("@NM", txtsuppliernama.Text);
    cmd.Parameters.AddWithValue("@AL", txtsupplieralamat.Text); 
    cmd.Parameters.AddWithValue("@EM", txtsupplieremail.Text);
    cmd.Parameters.AddWithValue("@TLP",txtsuppliernotelp.Text); 
    try
        {
            ...

4 个答案:

答案 0 :(得分:7)

确保连接字符串中的数据库名称全部为小写字母!我也得到了这个错误并使用this bug report解决了它,将我的数据库名称更改为所有小写字母。似乎此行为不依赖于目标系统类型。

答案 1 :(得分:2)

cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "call sp_insertSupplier(@KD,@NM,@AL,@EM,@TLP);";

如果CommandTypeStoredProcedure,则无需编写所有内容。只需写下:

cmd.CommandText = "sp_insertSupplier";

并像你一样提供参数。

确实没有名为call sp_insertSupplier(@KD,@NM,@AL,@EM,@TLP);

的过程/函数

答案 2 :(得分:2)

只需在CommandText中写下存储过程的名称

即可
  cmd.CommandText = "sp_insertSupplier";

在编写CommandText的方式中,您强制提供程序搜索名为“call sp_insertSupplier ......”的名称的storedprocedure,显然没有具有该名称的过程

答案 3 :(得分:0)

只需在commandText cmd.CommandText =“sp_insertSupplier”中写入存储过程的名称;你不需要“呼叫”部分