我基础不好,根据所学的知识,书本的例题修改的,也不知道我理解的是否符合老师要求

运用C#将数据存入数据库、并且可以读取数据库里的数据,此项目我运用了封装。我运用了一个窗体将数据存读数据。

我首先创建了一个数据库,库名“数学题库” ,然后创建了一个表“tiku1”,

添加一个类Class1

class Class1
    {
        public string strCon = @"Data Source=.;Initial Catalog=数学题库;Integrated security=true";
        public SqlConnection sqlcon = new SqlConnection();
        public SqlDataAdapter sda = new SqlDataAdapter();
        public DataSet ds = new DataSet();
        public DataTable dt = new DataTable();
        public SqlDataReader sdr;
        public void suanshu()
        {
            try
            {
                sqlcon = new SqlConnection(strCon);
            }
            catch (Exception e)
            {
                MessageBox.Show("数据库连接不成功:" + e.ToString());
            }
        }
        public void suanFill(string selstr)
        {
            dt.Clear();
            sda = new SqlDataAdapter(selstr, strCon);
            sda.Fill(ds,"tiku1");
            dt = ds.Tables["tiku1"];
        }
        public void suanselect(string showtnfo)
        {
            sqlcon.Open();
            SqlCommand sqlcmd = new SqlCommand(showtnfo, sqlcon);
            sdr = sqlcmd.ExecuteReader();
        }
        public void suanInsert(string insertinfo)
        {
            sqlcon.Open();
            SqlCommand sqlcmd = new SqlCommand(insertinfo, sqlcon);
            try
            {
                sqlcmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                MessageBox.Show("数据插入失败:" + e.ToString());
            }
            sqlcon.Close();
        }
        public void suanGridViewUpd()
        {
            SqlCommandBuilder scb = new SqlCommandBuilder(sda);
            DialogResult result;
            result = MessageBox.Show("确定保存修改过的数据吗?", "操作提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (result == DialogResult.OK)
            {
                dt = ds.Tables["tuku1"];
                sda.Update(dt);
                dt.AcceptChanges();
            }
        }
        public void suanDelete(string delStr)
        {
            sqlcon.Open();
            SqlCommand sqlcmd = new SqlCommand(delStr, sqlcon);
            try
            {
                sqlcmd.ExecuteNonQuery();
                MessageBox.Show("数据删除成功!");
            }
            catch (Exception ex)
            {
                MessageBox.Show("数据删除失败" + ex.ToString());
            }
            sqlcon.Close();
        }
    }
}

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string selstr = @"select * from tiku1 ";
        Class1 db = new Class1();

private void Form1_Load(object sender, EventArgs e)
        {
            db.suanshu();
            db.suanFill(selstr);
            comboBox1.ValueMember = "序号";
            comboBox1.DataSource = db.dt.DefaultView;
        }

private void button4_Click(object sender, EventArgs e)
        {
            db.suanshu();
            db.suanFill(selstr);
            dataGridView1.DataSource = db.dt;
        }

private void button1_Click(object sender, EventArgs e)
        {
            db.suanshu();
            string insertinfo = "insert into tiku1(序号,第一个数,符号一,第二个数,符号二,第三个数,符号三,结果) values('" + comboBox1.Text + "','" + textBox1.Text +
            "','" + textBox2.Text +
            "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text +
            "','" + textBox7.Text +
            "')";
            db.suanInsert(insertinfo);
            db.suanFill(selstr);
            dataGridView1.DataSource = db.dt;
        }

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selinfo = "select 第一个数,符号一,第二个数,符号二,第三个数,符号三,结果 from tiku1 where 序号='" + comboBox1.Text.ToString().Trim() + "'";
            db.suanshu();
            db.suanselect(selinfo);
            while (db.sdr.Read())

{

textBox1.Text = db.sdr["第一个数"].ToString();

textBox2.Text = db.sdr["符号一"].ToString();

textBox3.Text = db.sdr["第二个数"].ToString();

textBox4.Text = db.sdr["符号二"].ToString();

textBox5.Text = db.sdr["第三个数"].ToString();

textBox6.Text = db.sdr["符号三"].ToString();

textBox7.Text = db.sdr["结果"].ToString();

}

db.sdr.Close();
        }

private void button3_Click(object sender, EventArgs e)
        {
            db.suanshu();
            string strupd = "delete from tiku1 where 序号='" + comboBox1.Text.Trim() + "'";
            db.suanDelete(strupd);
          
            db.suanFill(selstr);
            dataGridView1.DataSource = db.dt;
        }

private void button2_Click(object sender, EventArgs e)
        {
            db.suanshu();
            db.suanGridViewUpd();
        }

}

}

说明

每道题我都编辑了序号,可以根据序号选题,改题,(序号我设置了下拉列表)

1、点击查询可以查询数据库里表里数据

2、点击添加可以添加数据

3、删除数据,更新数据

运用.NIT将数据存入数据库、读取数据库(运用封装)陈老师作业的更多相关文章

  1. 大数据学习day25------spark08-----1. 读取数据库的形式创建DataFrame 2. Parquet格式的数据源 3. Orc格式的数据源 4.spark_sql整合hive 5.在IDEA中编写spark程序(用来操作hive) 6. SQL风格和DSL风格以及RDD的形式计算连续登陆三天的用户

    1. 读取数据库的形式创建DataFrame DataFrameFromJDBC object DataFrameFromJDBC { def main(args: Array[String]): U ...

  2. pyspider爬取数据存入es--2.测试数据库连通性

    写一个简单案例测试能否将数据写入es #!/usr/bin/env python # -*- encoding: utf-8 -*- # Created on 2017-10-27 08:35:57 ...

  3. pyspider爬取数据存入mysql--2.测试数据库能否连通

    做一个简单的测试,看数据能否存入mysql 1 #!/usr/bin/env python 2 # -*- encoding: utf-8 -*- 3 # Created on 2017-10-26 ...

  4. pyspider爬取数据存入redis--2.测试数据库连通性

    直接上代码 #!/usr/bin/env python # -*- encoding: utf-8 -*- # Created on 2017-10-27 09:56:50 # Project: re ...

  5. 解析xml数据存入bean映射到数据库的 需求解决过程

    解析xml数据存入bean映射到数据库的 需求解决过程2017年12月19日 15:18:57 守望dfdfdf 阅读数:419 标签: xmlbean 更多个人分类: 工作 问题编辑版权声明:本文为 ...

  6. 读取数据库数据,并将数据整合成3D饼图在jsp中显示

    首先我将生成饼图的方法独立写成一个PieChar.java类,详细代码如下:(数据库需要自己建,如有需要的话) import java.io.IOException; import java.sql. ...

  7. 10天学会phpWeChat——第三天:从数据库读取数据到视图

    在第二天,我们创建了我们的第一个phpWeChat功能模块,但是比较简单.实际生产环境中,我们不可能有如此简单的需求.更多的情况是数据存储在MySql数据库中,我们开发功能模块的作用就是将这些数据从M ...

  8. 解决:HTML中多文本域(textarea)回车后数据存入数据库,EL表达式取出异常。

    问题描述: 当多文本域(textarea)回车后数据存入数据库. EL表达式取出异常,值换行倒置页面报错. 问题解决: 存值脚本代码,提交前转换\n为<br/>. <script t ...

  9. 编写SqlHelper使用,在将ExecuteReader方法封装进而读取数据库中的数据时会产生Additional information: 阅读器关闭时尝试调用 Read 无效问题,解决方法与解释

    在自学杨中科老师的视频教学时,拓展编写SqlHelper使用,在将ExecuteReader方法封装进而读取数据库中的数据时 会产生Additional information: 阅读器关闭时尝试调用 ...

随机推荐

  1. hadoop-2.7.1基于QMJ高可用安装配置

    1.修改主机名及hosts文件 10.205.22.185 nn1 (主)作用namenode,resourcemanager,datanode,JournalNode,zk,zkfc(hive,sq ...

  2. Win2003打不开https的问题

    碰到客户做问题是能打开https://www.baidu.com 这个网页 打不开 https://sha256.alipay.com/SHA256/index.htm支付宝这个网页      解决办 ...

  3. jquery click & get value of attributes of a href

    http://stackoverflow.com/questions/6625667/jquery-click-get-value-of-attributes-of-a-href /* Add a l ...

  4. PHP变量作用域以及地址引用问题

    作用域的概念: 在PHP脚本的任何位置都可以声明变量,但是,声明变量的位置会大大影响访问变量的范围.这个可以访问的范围称为作用域. 主要的常用的包括:局部变量.全局变量.静态变量. 1.局部变量:就是 ...

  5. php循环创建目录

    代码取自thinkphp中: function mk_dir($dir, $mod = 0777) { if(!is_dir($dir) || mkdir($dir, $mod)) { if(!mk_ ...

  6. WinSock编程基础

    一.套接字模式   1.阻塞模式       创建套接字时,默认是阻塞模式,对recv函数调用会使程序进入等待状态,知道接收到数据才返回.   2.非阻塞模式:      可以调用ioctlsocke ...

  7. IOS笔记 : addChildViewController

    一下addChildViewController,一个ViewController可以添加多个子ViewController,但是这 些子ViewController只有一个是显示到父视图中的,可以通 ...

  8. 公钥私钥 ssl/tsl的概念

    一,公钥私钥1,公钥和私钥成对出现2,公开的密钥叫公钥,只有自己知道的叫私钥3,用公钥加密的数据只有对应的私钥可以解密4,用私钥加密的数据只有对应的公钥可以解密5,如果可以用公钥解密,则必然是对应的私 ...

  9. Maven系列--pom.xml 配置详解

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  10. myeclipse激活+Aptana安装配置

    一.Myeclipse安装激活. 安装过程一路向下. 1.破解公钥,确保MyEclipse没有开启,否则失败! 用WinRAR或7-zip打开安装目录下Common\plugins\com.genui ...