2017-04-04 38 views
0

我希望从mysql数据库文件中选择id为2,4,6的记录数组。我试图将一个整数数组传递给一个Mysql存储过程。但是我无法创建一个工作存储过程。如何将参数数组传递给MySql select ... in

你能帮我写作吗?

这是C#代码

public static List<PhotoComment> GetPhotos(int[] _ids) 
    { 
     MySqlConnection _con = Generals.GetConnnection(); 
     List<PhotoComment> _comments = new List<PhotoComment>(); 
     try 
     { 
      MySqlCommand _cmd = new MySqlCommand("Photos_GetPhotosByIDs", _con); 
      _cmd.CommandType = CommandType.StoredProcedure; 
      _cmd.Parameters.AddWithValue("@_ids", _ids); 

      _con.Open(); 
      MySqlDataReader _reader = _cmd.ExecuteReader(); 
      while (_reader.Read()) 
      { 
       PhotoComment _comment = new PhotoComment(_reader.GetInt32("cID"), _reader.GetInt32("cFID"), _reader.GetString("cUser"), _reader.GetString("cContent"), _reader.GetDateTime("cDate"), _reader.GetBoolean("cChecked")); 
       _comments.Add(_comment); 
      } 
      _con.Close(); 
     } 
     catch (Exception _ex) 
     { 
      _con.Close(); 
      ReportMgr.ReportException(_ex.Message); 
     } 
     return _comments; 
    } 

这是MySQL

CREATE DEFINER = root @localhost PROCEDURE Photos_GetPhotosByIDs(在_ids INT [])

BEGIN

选择*从tbl_photos其中ID在ID中;

END

+0

你可以与我们分享任何代码? –

回答

0

MySQL中没有数组类型。您可以改用表格(临时表格)。使用id值填充大包并连接表以获得期望的结果。

0

我不确定我是否正确地得到你的问题,但我想你问的是如何将多个值发送到存储过程的输入参数。

如果这就是你要求的,下面的链接可能会有所帮助。不确定这样的功能是否存在于MySql中,但在SQL Server中,它被称为“Table-Valued Parameters” - 简称TVP。

TVP允许您将多个值传递到单个输入参数中。希望这有助于:https://msdn.microsoft.com/en-us/library/bb675163(v=vs.110).aspx

相关问题