2010-07-12 24 views

回答

1

http://forums.asp.net/p/988462/1278686.aspx

MySqlCommand cmd = new MySqlCommand("DeleteMessage", new MySqlConnection(GetConnectionString())); 
    cmd.CommandType = CommandType.StoredProcedure; 
    cmd.Parameters.Add(new MySqlParameter("param1", MessageItem.Entry_ID)); 
    cmd.Connection.Open(); 
    int i = cmd.ExecuteNonQuery(); 
    cmd.Connection.Close(); 
+0

@ratty:在'i'你。 – Svisstack 2010-07-12 12:27:57

6

这几乎等同于你将如何调用存储过程的SQL Server:

using(MySqlConnection conn = new MySqlConnection(connString)) 
{ 
    MySqlCommand command = new MySqlCommand("spSomeProcedure;", conn); 
    command.CommandType = System.Data.CommandType.StoredProcedure; 

    // Add your parameters here if you need them 
    command.Parameters.Add(new MySqlParameter("someParam", someParamValue)); 

    conn.Open(); 

    int result = (int)command.ExecuteScalar(); 
} 
0

Stored routines

存储函数和存储过程调用以不同的方式。

存储函数在SQL语句中用作常规函数。 例如

SELECT id, title, my_function(price) FROM table 

使用CALL语句调用存储过程。

CALL my_procedure(1,2,'title'); 

我不知道C#,所以可能你可以使用MySqlCommand类来调用存储过程,但是你不能用它来调用存储函数。

0

我实际上无法获得其他建议返回值的方法。我结束了创建一个字符串调用的函数,然后执行该字符串与.ExecuteScalar:

MySqlTransaction mySqlTransaction = testDataMySqlConnection.BeginTransaction(); 

mySqlCommand = new MySqlCommand 
    { 
    Connection = testDataMySqlConnection, 
    CommandText = "SELECT sf_UnitsAttempted('" + ... + ");", 
    CommandType = CommandType.Text 
    }; 

var f = (float)mySqlCommand.ExecuteScalar(); 
mySqlCommand.Dispose(); 
return f; 
0

我知道这个问题是关于从存储函数返回,这里贾斯汀的回答涵盖了。我想补充一点,如果你想从一个存储过程返回一个DataTable相反,你可以使用DataAdapter做到这一点:

// using MySql.Data.MySqlClient; // remember to include this 

/* Helper method that takes in a Dictionary list of parameters, 
    and returns a DataTable. 
    The connection string is fetched from a resources file. */ 
public static DataTable ExecuteProc(string procedureName, Dictionary<string,object> parameterList) 
{ 
    DataTable outputDataTable; 

    using (MySqlConnection MySqlConnection = new MySqlConnection(Resources.SQL_CONNECTION_STRING)) 
    { 
     using (MySqlCommand sqlCommand = new MySqlCommand(procedureName, MySqlConnection)) 
     { 
      sqlCommand.CommandType = CommandType.StoredProcedure; 

      if (parameterList != null) 
      { 
       foreach(string key in parameterList.Keys) 
       { 
        string parameterName = key; 
        object parameterValue = parameterList[key]; 

        sqlCommand.Parameters.Add(new MySqlParameter(parameterName, parameterValue)); 
       } 
      } 

      MySqlDataAdapter sqlDataAdapter = new MySqlDataAdapter(sqlCommand); 
      DataSet outputDataSet = new DataSet(); 
      sqlDataAdapter.Fill(outputDataSet, "resultset"); 

      outputDataTable = outputDataSet.Tables["resultset"]; 
     } 
    } 

    return outputDataTable; 
}