调用其他函数中的函数来访问C#中的数据库中的值

时间:2018-02-04 02:21:47

标签: c# sql postgresql user-interface web-applications

我正在尝试创建一个简单的应用程序,它连接PostgreSQL数据库,以便根据州和城市获得业务。在我再进一步之前,我想为任何含糊不清,措词不好或简单的无知道歉,因为这是我第一次使用C#。

应用程序查询的PostgreSQL中存储了一个名为business的表。我正在尝试实现这些SQL语句。

SELECT DISTINCT state
FROM business
ORDER BY state;

SELECT DISTINCT city
FROM business
WHERE state= "selected state"
ORDER BY city;

SELECT name
FROM business
WHERE city= "selected city" AND state= "selected state";

总的来说,应用程序看起来像

this

我能够实现select语句来获取状态和城市,但是我很难将两者连接在一起用于order by和where子句。我遇到的主要问题是获得州,以便它只显示州内的城市。与业务选择相同,我无法同时选择城市和州,以便只显示城市和州的业务。

以下是目前的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Npgsql;

namespace Milestone1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public class Business
        {
            public string name { get; set; }
            public string state { get; set; }
            public string city { get; set; }
        }
        public MainWindow()
        {
            InitializeComponent();
            addStates();
            addCities();
            addColumns2Grid();
        }
        private string buildConnString()
        {
            return "Host=localhost; username=postgres; Database=Milestone1DB";
        }


        public void addStates()
        {
            using (var conn = new NpgsqlConnection(buildConnString()))
            {
                conn.Open();
                using (var cmd = new NpgsqlCommand())
                {
                    cmd.Connection = conn;
                    cmd.CommandText = "SELECT distinct state FROM business ORDER BY state;";
                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            while (reader.Read()) {
                                statelist.Items.Add(reader.GetString(0));

                            }
                        }
                    }
                }
                conn.Close();

            }
        }
        public void addCities()
        {
            using (var conn = new NpgsqlConnection(buildConnString()))
            {
                conn.Open();
                using (var cmd = new NpgsqlCommand())
                {
                    Business b = new Business();
                    cmd.Connection = conn;
                    cmd.CommandText = "SELECT distinct city FROM business WHERE state = '" + b.state.ToString() + "';"; ##Problem is with this line
                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            while (reader.Read())
                            {
                                citylist.Items.Add(reader.GetString(0));
                                statelist.Items.Add(reader.GetString(1));


                            }
                        }
                    }
                }
                conn.Close();

            }
        }

        public void addColumns2Grid()
        {
            DataGridTextColumn col1 = new DataGridTextColumn();
            col1.Header = "Business Name";
            col1.Binding = new Binding("name");
            col1.Width = 255;
            businessGrid.Columns.Add(col1);

            DataGridTextColumn col2 = new DataGridTextColumn();
            col2.Header = "State";
            col2.Binding = new Binding("state");
            businessGrid.Columns.Add(col2);

            DataGridTextColumn col3 = new DataGridTextColumn();
            col3.Header = "City";
            col3.Binding = new Binding("City");
            businessGrid.Columns.Add(col3);



        }
        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void statelist_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            businessGrid.Items.Clear();
            if (statelist.SelectedIndex > -1)
            {
                using (var conn = new NpgsqlConnection(buildConnString()))
                {
                    conn.Open();
                    using (var cmd = new NpgsqlCommand())
                    {
                        cmd.Connection = conn;
                        cmd.CommandText = "SELECT name,state FROM business WHERE state = '" + statelist.SelectedItem.ToString()+ "';";
                        using (var reader = cmd.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                while (reader.Read())
                                {
                                    businessGrid.Items.Add(new Business() { name = reader.GetString(0), state = reader.GetString(1) });

                                }
                            }
                        }
                    }
                    conn.Close();

                }
            }
        }

        private void citylist_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            businessGrid.Items.Clear();
            if (citylist.SelectedIndex > -1)
            {
                using (var conn = new NpgsqlConnection(buildConnString()))
                {
                    conn.Open();
                    using (var cmd = new NpgsqlCommand())
                    {
                        Business b = new Business();
                        cmd.Connection = conn;
                        cmd.CommandText = "SELECT name,state,city FROM business WHERE state = '" + b.state.ToString() + " ORDER BY " + citylist.SelectedItem.ToString() + ";"; ##Problem is with this line
                        using (var reader = cmd.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                while (reader.Read())
                                {
                                    businessGrid.Items.Add(new Business() { name = reader.GetString(0), state = reader.GetString(1), city = reader.GetString(2) });

                                }
                            }
                        }
                    }
                    conn.Close();

                }
            }
        }
    }
}

我再次为自己对这个问题缺乏了解而道歉。任何建议都表示赞赏。

感谢您的阅读。

1 个答案:

答案 0 :(得分:1)

当用户被选为州时,您可以使用addCities()来设置城市。

因此,您可以在州的DropdownList中添加onchange事件,然后获取用户的州值并创建动态城市列表。

BTW我可能会使用Paramter来阻止SQLInjection。

编辑:我重写了你的代码,可能会有一些错误。 我还为你添加了一些注释:)。希望可以帮到你。

private void statelist_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    businessGrid.Items.Clear();
    //clear your citylist,if you selected other state.
    citylist.Items.Clear();
    if (statelist.SelectedIndex > -1)
    {
        using (var conn = new NpgsqlConnection(buildConnString()))
        {
            conn.Open();
            using (var cmd = new NpgsqlCommand())
            {
                cmd.Connection = conn;
                cmd.CommandText = "SELECT name,state FROM business WHERE state = '" + statelist.SelectedItem.ToString()+ "';";
                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        while (reader.Read())
                        {
                            businessGrid.Items.Add(new Business() { name = reader.GetString(0), state = reader.GetString(1) });

                        }
                    }
                }
            }
            conn.Close();
        }
        //create cities list by statelist's value
        addCities();
    }
}



private void citylist_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    businessGrid.Items.Clear();
    if (citylist.SelectedIndex > -1)
    {
        using (var conn = new NpgsqlConnection(buildConnString()))
        {
            conn.Open();
            using (var cmd = new NpgsqlCommand())
            {
                //Business b = new Business(); you do not need create a Business model
                //you need to get user selected state's value to query.
                string statelist = statelist.SelectedItem.ToString(); 
                cmd.Connection = conn;
                cmd.CommandText = "SELECT name,state,city FROM business WHERE state = '" + statelist + " ORDER BY " + citylist.SelectedItem.ToString() + ";"; ##Problem is with this line
                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        while (reader.Read())
                        {
                            businessGrid.Items.Add(new Business() { name = reader.GetString(0), state = reader.GetString(1), city = reader.GetString(2) });

                        }
                    }
                }
            }
            conn.Close();
        }
    }
}


public void addCities()
{
    using (var conn = new NpgsqlConnection(buildConnString()))
    {
        conn.Open();
        using (var cmd = new NpgsqlCommand())
        {
            //Business b = new Business(); you do not need create a Business model
            //you need to get user selected state's value to query.
            string statelist = statelist.SelectedItem.ToString(); 

            cmd.Connection = conn;
            cmd.CommandText = "SELECT distinct city FROM business WHERE state = '" + statelist.SelectedItem.ToString() + "';"; ##Problem is with this line
            using (var reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    while (reader.Read())
                    {
                        citylist.Items.Add(reader.GetString(0));
                        //statelist.Items.Add(reader.GetString(1)); There is not 2 field in your sql syntans
                    }
                }
            }
        }
        conn.Close();

    }
}

public MainWindow()
{
    InitializeComponent();
    addStates();
    addColumns2Grid();
}