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 WindowsFormsApplication2
{
public partial class mainform : Form
{
public mainform()
{
InitializeComponent();
}
private void 新增固定资产ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form1 f = new Form1();
f.MdiParent = this;
f.Show();
}
private void 查询固定资产ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.MdiParent = this;
f.Show();
}
}
}
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == null || textBox2.Text == null || textBox3.Text == null || comboBox1.Text.Equals("请选择类型"))
{
MessageBox.Show("请输入完整信息", "信息提示", MessageBoxButtons.OK);
}
else {
string s = "Data Source=.;Initial Catalog=assetdb;Integrated Security=True";
SqlConnection con = new SqlConnection(s);
con.Open();
try
{
string sql = "insert into assets values('" + textBox1.Text + "','" + textBox2.Text + "','" + comboBox1.Text + "','" + textBox3.Text + "')";
SqlCommand com = new SqlCommand(sql, con);
int b = com.ExecuteNonQuery();
if (b == 1)
{
MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK);
}
else
{
MessageBox.Show("添加失败", "提示", MessageBoxButtons.OK);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally {
con.Close();
}
}
}
}
}
namespace WindowsFormsApplication2
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
DBHelper db = new DBHelper();
DataSet ds = new DataSet();
string sql = "select * from assets";
SqlDataAdapter a = new SqlDataAdapter(sql,db.Connection);
a.Fill(ds,"assets");
this.dataGridView1.DataSource = ds.Tables["assets"];
}
public class DBHelper
{
// 数据库连接字符串
private string connString = @"Data Source=.;Initial Catalog=assetdb;Integrated Security=True";
// 数据库连接 Connection 对象
private SqlConnection connection;
/// <summary>
/// Connection对象
/// </summary>
public SqlConnection Connection
{
get
{
if (connection == null)
{
connection = new SqlConnection(connString);
}
return connection;
}
}
/// <summary>
/// 打开数据库连接
/// </summary>
public void OpenConnection()
{
if (Connection.State == ConnectionState.Closed)
{
Connection.Open();
}
else if (Connection.State == ConnectionState.Broken)
{
Connection.Close();
Connection.Open();
}
}
/// <summary>
/// 关闭数据库连接
/// </summary>
public void CloseConnection()
{
if (Connection.State == ConnectionState.Open || Connection.State == ConnectionState.Broken)
{
Connection.Close();
}
}
}
private void button1_Click(object sender, EventArgs e)
{
DBHelper db = new DBHelper();
DataSet ds = new DataSet();
try
{
string sql = "select * from assets";
if (textBox1.Text == "" && comboBox1.Text.Equals("请选择类型"))
{
sql = "select * from assets";
}
else if (textBox1.Text == "")
{
sql = "select * from assets where assettype='" + comboBox1.Text + "'";
}
else if (comboBox1.Text.Equals("请选择类型"))
{
sql = "select * from assets where assetid='" + textBox1.Text + "'";
}
else{
sql = "select * from assets where assettype='" + comboBox1.Text + "' and assetid='" + textBox1.Text + "'";
}
SqlDataAdapter a = new SqlDataAdapter(sql,db.Connection);
a.Fill(ds, "assets");
this.dataGridView1.DataSource = ds.Tables["assets"];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}