C#可视化 房产中介查询系统(具体做法及全部代码)

目录

题目:

 效果图:

 数据库:

做法:

datagirdview设置

全部代码: 

 DBHelper类

 From1主窗体代码

 添加页面代码


题目:

 

 

 

 

 效果图:

                                           

 数据库:

 

做法:

进行查询datagirdview时, 表里的状态时0,1这时我们就用到了case when than else end

            string sql = string.Format("select HouseID,HouseName,HouseAddress,(case when HouseState=1 then'在售' else'售罄' end)HouseState,AvgPrice,Tel  from Housing");
 

datagirdview设置

 

首先设置datagridview的这三个属性

    1. AutoSizeColumsMode = Fill 设置每列单元格宽度平铺

    1. RowHeadersVisible = False 取消列表最左侧列显示

    1. SelectionMode = FullRowSelect 设置单元格选中模式为整行选中

全部代码: 

 DBHelper类

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Fangchan
{
    internal class DBHelper
    {
        public static string connStr = "server=.;database=LettingAgent;uid=sa;pwd=123456";
        public static SqlConnection conn = null;
        public static void indb()
        {
            if (conn == null)
            {
                conn = new SqlConnection(connStr);

            }

            conn.Close();
            conn.Open();


        }
        public static DataSet Query(string sql)
        {
            indb();
            DataSet ds = new DataSet();
            try
            {
                SqlDataAdapter ada = new SqlDataAdapter(sql, conn);

                ada.Fill(ds);
            }
            catch (Exception)
            {


            }

            conn.Close();
            return ds;

        }
        public static bool NoQuery(string sql)
        {
            indb();
            SqlCommand cmd = new SqlCommand(sql, conn);
            int ret = cmd.ExecuteNonQuery();
            conn.Close();
            if (ret > 0)
            {
                return true;
            }
            else
                return false;
        }
    }
}

 From1主窗体代码

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

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

        private void Form1_Load(object sender, EventArgs e)
        {
            string sql = string.Format("select HouseID,HouseName,HouseAddress,(case when HouseState=1 then'在售' else'售罄' end)HouseState,AvgPrice,Tel  from Housing");
            DataSet ds = DBHelper.Query(sql);
            this.dataGridView1.DataSource = ds.Tables[0];
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (comboBox1.Text == "所有")
            {
                string sql = string.Format("select HouseID,HouseName,HouseAddress,(case when HouseState=1 then'在售' else'售罄' end)HouseState,AvgPrice,Tel  from Housing ", comboBox1.Text);
                DataSet ds = DBHelper.Query(sql);
                this.dataGridView1.DataSource = ds.Tables[0];
            }
            if (comboBox1.Text == "在售")
            {
                string sql = string.Format("select HouseID,HouseName,HouseAddress,(case when HouseState=1 then'在售' else'售罄' end)HouseState,AvgPrice,Tel  from Housing where HouseState=1", comboBox1.Text);
                DataSet ds = DBHelper.Query(sql);
                this.dataGridView1.DataSource = ds.Tables[0];
            }
            if (comboBox1.Text == "售罄")
            {
                string sql = string.Format("select HouseID,HouseName,HouseAddress,(case when HouseState=1 then'在售' else'售罄' end)HouseState,AvgPrice,Tel  from Housing where HouseState=2", comboBox1.Text);
                DataSet ds = DBHelper.Query(sql);
                this.dataGridView1.DataSource = ds.Tables[0];
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            添加房源 jia = new 添加房源();
            jia.ShowDialog();
            string sql = string.Format("select HouseID,HouseName,HouseAddress,(case when HouseState=1 then'在售' else'售罄' end)HouseState,AvgPrice,Tel  from Housing");
            DataSet ds = DBHelper.Query(sql);
            this.dataGridView1.DataSource = ds.Tables[0];
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}

 添加页面代码

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

namespace Fangchan
{
    public partial class 添加房源 : Form
    {
        public 添加房源()
        {
            InitializeComponent();
        }

        private void 添加房源_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.textBox1.Text=="")
            {
                MessageBox.Show("名称不能为空");
                return;
            }
            if (this.textBox2.Text == "")
            {
                MessageBox.Show("地址不能为空");
                return;
            }

            if (this.textBox3.Text == "")
            {
                MessageBox.Show("电话不能为空");
                return;
            }
            string HouseState = string.Empty;
            if (radioButton1.Checked==true)
            {
                HouseState="1";
            }
            else if (radioButton2.Checked==true)
            {
                HouseState = "2";
            }

            if (this.textBox4.Text == "")
            {
                MessageBox.Show("均价不能为空");
                return;
            }
            string sql = string.Format("insert Housing values ('{0}','{1}','{2}','{3}','{4}')",textBox1.Text,textBox2.Text,textBox3.Text,HouseState.ToString(),textBox4.Text);
            if (DBHelper.NoQuery(sql) == true)
            {
                MessageBox.Show("添加成功");

            }
            else
                MessageBox.Show("添加失败");
        }
    }
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

W少年没有乌托邦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值