如何从选择语句中分离出检索到的信息

时间:2019-03-27 04:29:52

标签: c# mysql

我当前正在尝试从sql数据库中获取信息并将其分离,因此它显示信息如下: 1) 2) 3) 等等...

现在,当我运行该程序时,歌曲标题又回到了自己的一行,但是我希望它们编号。我用选择语句提取歌曲标题并将其存储在字符串中。我该如何选择要在写作行中加上的标题?我有办法将其存储在字符串数组中吗?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using System.Data;
namespace Databasetesting
{
    class Play
    {

        //finding an artists
        public static void FindArtist()
        {
            //servere connection
            string cs = @"server=192.168.0.10;userid=dbsAdmin1903;password=password;database=Music_Mixer;port=8889";
            MySqlConnection conn = null;
            MySqlDataReader rdr = null;

            //prompt for artist
            string Art = PromptArtist();

            try
            {
                //call in database
                conn = new MySqlConnection(cs);

                //open connection
                conn.Open();

                //Statement
                String cmdText = "SELECT song_title FROM songs WHERE artist = @uI LIMIT 15";

                //make a new command
                MySqlCommand cmd = new MySqlCommand(cmdText, conn);

                //binding
                cmd.Parameters.AddWithValue("@uI", Art);

                //make reader = to new command
                rdr = cmd.ExecuteReader();

               //if something is not found
                while (!rdr.HasRows)
                {
                    Console.WriteLine("\r\nSorry, we could find that Artist.");
                    Console.WriteLine("Would you like to try again?!");
                    Menu.menu();
                }

                //run the reader and display to user
                Console.Clear();

                Console.WriteLine($"Here are 15 songs by {Art}!");

                while (rdr.Read())
                {
                    //store song titles and display to user
                    string Songs;

                    Songs = rdr["Song_title"].ToString();

                    Console.WriteLine (Songs);

                }
                //run the play again method
                Console.Write("Press ENTER to continue...");
                Console.ReadLine();
                Console.WriteLine("What would you like to do?");


                Menu.again();

            }
            catch (MySqlException er)
            {
                Console.WriteLine(er);
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
                Console.ReadLine();
            }
        }


}

2 个答案:

答案 0 :(得分:0)

如果我不理解正确

List<string> songsTitles = new List<string>();
while (rdr.Read())
{
    //store song titles and display to user
    string song = rdr["Song_title"].ToString();
    Console.WriteLine(song);
    songsTitles.Add(song);
}

var numberedSongs = songsTitles.Select((obj, index) => new {Index = index, Obj = obj}).ToArray();
string[] numberedSongsString = songsTitles.Select((obj, index) => $"{index}) {obj}").ToArray();

答案 1 :(得分:0)

您可以使用辅助数据表,如下所示:

DataTable dt = new DataTable();
MySqlCommand cmd = new MySqlCommand(cmdText, conn);
dt.Load(cmd.ExecuteReader());
if(dt.Rows.Count != 15){
    //error code
}else{
    foreach(DataRow row in dt.Rows){
        Console.WriteLine(row["Song_title"].ToString());
    }
}