C#中的人口数据库…程序未运行,但没有错误

问题描述 投票:0回答:1

我正在从我的课本上做练习程序。我是新来的,所以对我来说光秃秃的。 我已经写了所有代码,除了我要运行它时,没有任何错误,它说这:

“ Visual Studio无法启动调试,因为缺少调试目标'C:\ Users \ Smith \ source \ repos \ Population_Database \ Population_Database \ bin \ Debug \ Population_Database.exe'。请生成项目,然后重试或设置OutputPath和适当地使用AssemblyName属性来指向目标程序集的正确位置。“

教科书说:

在示例程序的Chap11文件夹中,您将找到一个数据库文件。名为PopulationDB.mdf。该数据库有一个名为City的表。城市表有以下几列:列名数据类型城市nvarchar(50)主键人口流动“城市”列存储城市名称,而“人口”列存储城市名称该城市的人口。该数据库已经输入了20行。创建一个连接到PopulationDB.mdf数据库的应用程序,并允许用户执行以下操作:•使用数据绑定控件将新行添加到数据库,更改现有行行,然后删除行。•按人口升序对城市列表进行排序。•按人口降序对城市列表进行排序。•按名称对城市列表进行排序。•获取所有城市的总人口。•获取所有城市的平均人口。•获得最高人口。•获得最低的人口。

这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Population_Database
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

    }

    private void newrow_Click(object sender, EventArgs e)
    {
        SqlConnection cn = new SqlConnection();
        cn.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\PopulationDB.mdf;Integrated Security=True;User Instance=True";
        cn.Open();
        SqlDataAdapter adapter = new SqlDataAdapter("select * from City", cn);
        SqlCommandBuilder cmdb = new SqlCommandBuilder(adapter);
        DataSet ds = new DataSet("City");
        adapter.Fill(ds, "City");
        DataRow row = ds.Tables["City"].NewRow();
        row["City"] = textBox1.Text;
        row["Population"] = textBox2.Text;
        ds.Tables["City"].Rows.Add(row);
        adapter.Update(ds, "Income");
        cn.Close();
        MessageBox.Show("Details Entered Sucessfully ....");
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection cn = new SqlConnection();
        cn.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\PopulationDB.mdf;Integrated Security=True;User Instance=True";
        cn.Open();
        SqlCommand cmd = new SqlCommand("Select * from City ORDER BY Population ASC", cn);
        SqlDataReader sdr = cmd.ExecuteReader();
        while (sdr.Read())
        {
            String tot;
            comboBox1.SelectedItem = tot.ToString();

        }
    }

    private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection cn = new SqlConnection();
        cn.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\PopulationDB.mdf;Integrated Security=True;User Instance=True";
        cn.Open();
        SqlCommand cmd = new SqlCommand("Select * from City ORDER BY Population DESC", cn);
        SqlDataReader sdr = cmd.ExecuteReader();
        while (sdr.Read())
        {
            String tot;
            comboBox2.SelectedItem = tot.ToString();

        }
    }

    private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection cn = new SqlConnection();
        cn.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\PopulationDB.mdf;Integrated Security=True;User Instance=True";
        cn.Open();
        SqlCommand cmd = new SqlCommand("Select City from City ", cn);
        SqlDataReader sdr = cmd.ExecuteReader();
        while (sdr.Read())
        {
            String tot;
            comboBox1.SelectedItem = tot.ToString();

        }
    }

    private void textBox3_TextChanged(object sender, EventArgs e)
    {
        SqlConnection cn = new SqlConnection();
        cn.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\PopulationDB.mdf;Integrated Security=True;User Instance=True";
        cn.Open();
        SqlCommand cmd = new SqlCommand("select SUM(Population) from City", cn);
        SqlDataReader sdr = cmd.ExecuteReader();
        while (sdr.Read())
        {
            double tot;
            tot = Convert.ToDouble(sdr.GetSqlValue(0).ToString());
            textBox3.Text = tot.ToString();
        }
    }

    private void textBox4_TextChanged(object sender, EventArgs e)
    {
        SqlConnection cn = new SqlConnection();
        cn.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\PopulationDB.mdf;Integrated Security=True;User Instance=True";
        cn.Open();
        SqlCommand cmd = new SqlCommand("select AVG(Population) from City", cn);
        SqlDataReader sdr = cmd.ExecuteReader();
        while (sdr.Read())
        {
            double tot;
            tot = Convert.ToDouble(sdr.GetSqlValue(0).ToString());
            textBox4.Text = tot.ToString();

        }
    }

    private void textBox5_TextChanged(object sender, EventArgs e)
    {
        SqlConnection cn = new SqlConnection();
        cn.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\PopulationDB.mdf;Integrated Security=True;User Instance=True";
        cn.Open();
        SqlCommand cmd = new SqlCommand("select MAX(Population) from City", cn);
        SqlDataReader sdr = cmd.ExecuteReader();
        while (sdr.Read())
        {
            double tot;
            tot = Convert.ToDouble(sdr.GetSqlValue(0).ToString());
            textBox5.Text = tot.ToString();
        }
    }

    private void textBox6_TextChanged(object sender, EventArgs e)
    {
        SqlConnection cn = new SqlConnection();
        cn.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\PopulationDB.mdf;Integrated Security=True;User Instance=True";
        cn.Open();
        SqlCommand cmd = new SqlCommand("select MIN(Population) from City", cn);
        SqlDataReader sdr = cmd.ExecuteReader();
        while (sdr.Read())
        {
            double tot;
            tot = Convert.ToDouble(sdr.GetSqlValue(0).ToString());
            textBox6.Text = tot.ToString();
        }
    }
}
}

我想念什么?

c#
1个回答
0
投票

从评论中进行:您的文本框将不会显示任何值,因为您没有将tot分配给字符串tot行中的任何内容,只是声明了。

Tot = sdr.item [无论您要显示其值的列名都将解决您未分配的问题。

© www.soinside.com 2019 - 2024. All rights reserved.