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)
            {
    
            }
        }
    }
  • 相关阅读:
    Oracle:DBMS_STATS.GATHER_TABLE_STATS的语法
    Oracle Purge和drop的区别
    Oracle Pipelined Table Functions简介
    Firefox与IE浏览器缓存的两个重要区别
    @SuppressWarnings的使用、作用、用法
    正确优雅地解决用户退出——JSP及Struts解决方案
    log4j详解与实战
    maven项目在eclipse中debug时看不到源码?
    maven tomcat eclipse 配置 debug
    Java泛型详解
  • 原文地址:https://www.cnblogs.com/alfredsun/p/4462573.html
Copyright © 2011-2022 走看看