visual studio 2017 i mysql

0

jak polaczyc sie z baza ?

windows forms

0

tyle umiem :)

0

ale tu już nie dotarłeś https://www.google.pl/search?q=c%23+mysql

0

Witam,

Zacznij może od analizy tego:

SELECT * FROM cityCREATE TABLE `city` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(128) COLLATE utf8_polish_ci NOT NULL,
  `county` varchar(128) COLLATE utf8_polish_ci NOT NULL,
  `population` int(11) NOT NULL,
  `street` varchar(128) COLLATE utf8_polish_ci NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8 COLLATE=utf8_polish_ci;
namespace MysqlConnectQuickSample
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using MySql.Data.MySqlClient;

    public static class MysqlConnectQuickSampleApp
    {
        private const string ConnectionString = "https://www.connectionstrings.com/mysql/";

        public static void Main()
        {
            Console.Write("Select City: ");
            Console.WriteLine(string.Join(", ", GetCities()));

            string city = Console.ReadLine();

            var record = GetRecordByName(city);

            if (record != null)
            {
                Console.WriteLine(record);
            }

            Console.ReadLine();
        }

        public static IEnumerable<string> GetCities()
        {
            var cities = new List<string>();

            try
            {
                using (var connection = new MySqlConnection(ConnectionString))
                {
                    connection.Open();

                    using (var command = connection.CreateCommand())
                    {
                        command.CommandText = "SELECT name FROM city;";

                        using (var reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                string city = reader.GetString(reader.GetOrdinal("name"));
                                cities.Add(city);
                            }
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.Message);
            }

            return cities;
        }

        public static Record GetRecordByName(string name)
        {
            Record record = null;

            try
            {
                using (var connection = new MySqlConnection(ConnectionString))
                {
                    connection.Open();

                    using (var command = connection.CreateCommand())
                    {
                        command.CommandText = "SELECT name, county, population, street FROM city WHERE name = @name;";

                        command.Parameters.Add("@name", MySqlDbType.VarChar, name.Length).Value = name;

                        using (var reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                string county = reader.GetString(reader.GetOrdinal("county"));
                                int population = reader.GetInt32(reader.GetOrdinal("population"));
                                string street = reader.GetString(reader.GetOrdinal("street"));

                                record = new Record
                                {
                                    County = county,
                                    Name = name,
                                    Population = population,
                                    Street = street
                                };
                            }
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.Message);
            }

            return record;
        }
    }

    public class Record
    {
        public string Name { get; set; }

        public string County { get; set; }

        public int Population { get; set; }

        public string Street { get; set; }

        public override string ToString()
        {
            return $"{Name} in {County} ({Population})";
        }
    }
}

Pozdrawiam,

mr-owl

1 użytkowników online, w tym zalogowanych: 0, gości: 1