目录
题目:
效果图:
数据库:
做法:
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");
首先设置datagridview的这三个属性
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;
}
}
}
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("添加失败");
}
}
}