如何调用数据库,以便比较字符串?

时间:2019-03-16 20:32:45

标签: c# mysql sql visual-studio

我目前正在从事辅助Music Finder项目。在此应用程序中,用户将在控制台中输入歌词,然后控制台会返回艺术家和标题。我从sequel pro引入了一个数据库,因此我可以比较字符串并获得最接近的匹配。我想尝试使用.CompareTo()来查看它是否可以按照我想要的方式工作。我现在遇到的问题是我需要使用foreach,而且我不确定如何在声明中调用歌词。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;


namespace Databasetesting
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.WriteLine("Start\n");
            // MySQL Database Connection String
            string cs = @"server=192.168.0.5;userid=dbsAdmin1903;password=password;database= Music Mixer;port=8889";

            //set up connection
            MySqlConnection con = null;

            //make a reader
            MySqlDataReader  reader = null;

            Console.WriteLine("Please enter song lyrics:");
            string userInput = Console.ReadLine();

            //write a try catch statement   
            try
            {
                //cal in database
                con = new MySqlConnection(cs);

                //open connection
                con.Open();



                //Statement
                String cmdText = "SELECT Artist FROM Songs WHERE Title=@userInput";

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

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

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

                //run the reader and display to user
                while(reader.Read())
                {
                    //Where im having trouble
                    foreach(DataTable myTable in Music Mixer.tables)

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

0 个答案:

没有答案