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;
using System.Data.SqlClient;
namespace 客房信息
{
public partial class Form2 : Form
{
DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter();
string str = "Data Source=.;Initial Catalog=HotelDB;Integrated Security=True";
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
cha();
}
public void show()
{
string sql = @"SELECT
[Number],[Beds] ,[Price],[Remark]
FROM [HotelDB].[dbo].[Room]";
SqlConnection conn = new SqlConnection(str);
try
{
if (ds.Tables["room"] != null)
{
ds.Tables["room"].Clear();
}
conn.Open();
//SqlCommand comm = new SqlCommand(sql,conn);
adapter = new SqlDataAdapter(sql, conn);
adapter.Fill(ds, "room");
this.dataGridView1.DataSource = ds.Tables["room"];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
}
}
private void Form2_Load(object sender, EventArgs e)
{
show();
}
private void toolStripButton4_Click(object sender, EventArgs e)
{
this.Close();
}
public void cha()
{
string sql = @"SELECT
[Number]
,[Beds]
,[Price]
,[Remark]
FROM [HotelDB].[dbo].[Room]";
SqlConnection conn = new SqlConnection(str);
if (!this.textBox2.Text.Equals(""))
{
sql += " where Number=" + this.textBox2.Text;
}
adapter = new SqlDataAdapter(sql, conn);
try
{
if (ds.Tables["room"] != null)
{
ds.Tables["room"].Clear();
}
adapter.Fill(ds, "room");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
this.dataGridView1.DataSource = ds.Tables["room"];
}
public void show2()
{
this.kefang.Text = this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
this.chuang.Text = this.dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
this.jiage.Text = this.dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
this.beizhu.Text = this.dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
show2();
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
shanchu();
}
public void shanchu()
{
SqlConnection conn = new SqlConnection(str);
DialogResult rest = MessageBox.Show("确定要删除吗?","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Information);
if (rest==DialogResult.OK)
{
if (dataGridView1.SelectedRows.Count>0)
{
try
{
string nuber=this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
conn.Open();
string sql = string.Format("delete from room where Number='{0}'",nuber);
SqlCommand comm = new SqlCommand(sql,conn);
int i = comm.ExecuteNonQuery();
if (i==1)
{
MessageBox.Show("删除成功!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
实现模拟考试的答案
最新推荐文章于 2022-12-19 20:37:22 发布