S1模拟机试

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);
            }
        }  
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值