SQL i c#

0

Cześć,
Jestem w trakcie robienia programu w c# gdzie pobieram dane z SQL. Mam problem z pobraniem danych z bazy za pomocą argumentu programu.
Np. jako argument programu podaje miejscowość, a na standardowym wyjściu powinna wyświetlić mi się liczba mieszkańców, najdłuższa ulica.
Czy wie ktoś w jaki sposób to rozwiązać??

0

a co to jest ten "argument programu" ? Ogólnie pokaż kod co tam tworzysz, to Ci pewnie ktoś pomoże.

0

Argument programu to zmienna wejściowa

Mój kod:
class Program
{
static void Main(string[] args)
{
try
{
SqlConnection polaczenie = new SqlConnection("Data Source=DESKTOP-CAAKA5J\SQLEXPRESS;Initial Catalog=Miasta; Trusted_Connection=True");
polaczenie.Open();
SqlCommand komendaSQL = polaczenie.CreateCommand();
komendaSQL.CommandText = "SELECT Liczba,UlicaMAKS FROM SpisMiast";
Int32 count = (Int32)komendaSQL.ExecuteScalar();
SqlDataReader czytnik = komendaSQL.ExecuteReader();
string miasto = args[0];
string x;
x= czytnik.ToString();
while (czytnik.Read())
{
Console.WriteLine(Console.WriteLine("Informacje o danym mieście to {0} to: {1}",
miasto, czytnik, String.Compare(miasto, czytnik, true)));
}
czytnik.Close();
polaczenie.Close();

            Console.ReadKey();

        }
        catch (SqlException e)
        {
            Console.WriteLine("Błąd");
            Console.WriteLine(e.Message);
            Console.ReadKey();
        }
    }

Dokładne polecenie: Napisz program, który pobiera z tabeli SpisMiast w bazie Miasta liczbę mieszkańców oraz najdłuższą ulicę o podanej nazwie miasta przez użytkownika jako argument programu i wypisuje na wyjściu wynik.

1

Witam,

Doczytaj o co tutaj chodzi:

CREATE TABLE [dbo].[city](
	[id] [int] IDENTITY(1,1) PRIMARY KEY,
	[name] [nvarchar](128) NOT NULL,
	[county] [nvarchar](128) NOT NULL,
	[population] [int] NOT NULL,
	[street] [nvarchar](128) NOT NULL
)
namespace SqlConnectQuickSample
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;

    public static class SqlConnectQuickSampleApp
    {
        private const string ConnectionString = "https://www.connectionstrings.com/sql-server/";

        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 SqlConnection(ConnectionString))
                {
                    connection.Open();

                    using (var command = connection.CreateCommand())
                    {
                        command.CommandText = "SELECT name FROM [dbo].[city] WITH (nolock);";

                        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 SqlConnection(ConnectionString))
                {
                    connection.Open();

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

                        command.Parameters.Add("@name", SqlDbType.NVarChar, 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