【发布时间】:2018-07-14 05:11:59
【问题描述】:
我正在尝试创建一个与 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";
整个应用程序看起来像
.
我能够实现选择语句来获取州和城市,但在将两者链接在一起以获取 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();
}
}
}
}
}
再次为我对这个主题缺乏了解表示歉意。任何建议表示赞赏。
感谢您的阅读。
【问题讨论】:
标签: c# sql postgresql user-interface web-applications