• 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)
            {
    
            }
        }
    }
  • 相关阅读:
    Jquery 公告 滚动+AJAX后台得到数据。
    图片上添加文字。
    javascript的window.onload与jquery的$(document).ready()
    后台JS写法
    Ajax xmlhttprequest原理(一)
    SQL语句二次排序。先根据是否置顶字段。再根据最后更新时间排序。
    引以为戒的SQL语句写法
    C#中var关键字怎么用 ~
    上传文件 解析
    简易公告
  • 原文地址:https://www.cnblogs.com/alfredsun/p/4462573.html
Copyright © 2020-2023  润新知