zoukankan      html  css  js  c++  java
  • C# 实验4 数据库

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient; 
    namespace shiyan4
    {
        public partial class Form1 : Form
        {
            SqlDataAdapter adapter;
            DataTable table;
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                string varNumber = textBox1.Text.Trim();
                if (varNumber ==""||varNumber ==null){MessageBox.Show("请输入你要查询的学号", "提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);}
                try
                {string connStr = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True";
                    SqlConnection conn = new SqlConnection(connStr);
                    string sql = "select * from Table1 where 姓名='" + varNumber + "'";
                    adapter = new SqlDataAdapter(sql, conn);
                    SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
                 
                    
                    table = new DataTable();
                    adapter.Fill(table);
                    dataGridView1.DataSource = table;
                    //conn.Close();
             }
                catch (Exception ee)
                {
                    MessageBox.Show(ee.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                } 
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                textBox1.Text = "";
                dataGridView1.EndEdit();
                try
                {
                    adapter.Update(table);
                    MessageBox.Show("保存成功");
                }
                catch (Exception ee)
                {
                    MessageBox.Show(ee.Message, "保存失败");
                }
                
            }
    
            private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {
    
            }
        }
    }
  • 相关阅读:
    [HAOI2015]按位或——Min-Max容斥+FWT
    HDU 4773 Problem of Apollonius——圆反演
    类欧几里得小结
    线性规划(单纯形法)知识整理
    奇怪的数学题(51nod1847)——min_25筛+杜教筛+第二类斯特林数
    CSP-S 2019 游记
    2019.11.11~2019.11.12考试总结
    2019.11.6~2019.11.7考试总结
    1019.11.1~2019.11.2考试总结
    Final-阶段站立会议4
  • 原文地址:https://www.cnblogs.com/alfredsun/p/4462573.html
Copyright © 2011-2022 走看看