如何用select from c调用mysql存储过程#

luaexgnf  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(316)

谁能帮我一下吗。我没有经常使用c#,我需要用一个输入参数调用mysql存储过程,我需要写下它的查询输出。
我的存储过程:

CREATE DEFINER=`root`@`%` PROCEDURE `SP_For_Insert`(var_username varchar(50))

BEGIN

Declare counter int default 0;
Select Count(username) into counter From users where username = var_username;
if counter = 0  then
    select 0 as zero_results;
else 
    Select username from users where username=var_username;
end if;
END

我知道如何调用简单的查询和存储结果,但我需要这个工作。

q9yhzks0

q9yhzks01#

所以我找到了一个解决方案,如果其他人有这个问题,我现在就贴出来

//for connection parameters
  private static string connectionString = "datasource=localhost;"+
                                "port=3306;"+
                                "Initial Catalog='authorization_schema';"+
                                "username=admin;"+
                                "password=admin123;";

  MySqlConnection connection = new MySqlConnection(connectionString);

  // construct SQL command with parameters (this way you can call SP also)
  IDbCommand cmd = new MySqlCommand("SELECT account, action FROM AUTHORIZATIONS WHERE username=@UserName", connection);

  // construct parameters
  MySqlParameter usernameParam = new MySqlParameter();
  usernameParam.ParameterName = "@UserName";
  usernameParam.Value = username;

  cmd.Parameters.Add(usernameParam);

  / construct data adapter
  IDataAdapter adapter = new MySqlDataAdapter((MySqlCommand)cmd);

  // construct DataSet to store result
  DataSet ds = new DataSet();
  adapter.Fill(ds);

相关问题