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

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    //加入头文件
    using System.Data;
    using System.Data.OleDb;
    
    namespace 数据库遍历
    {
        class TraverseDB
        {
            private String constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";//"Data Source=2003.mdb";
            private String filpath;//文件路径
            private OleDbConnection connection;
            private OleDbDataAdapter oleAdapter;
            private DataSet dataSet = new DataSet();
            private String orderStr;
            private OleDbCommand cmd;
            private OleDbCommandBuilder cb;
            public TraverseDB()
            {
                filpath = "G:\C_SHAP_2014\数据库遍历\数据库遍历\bin\Debug\2004.mdb";
                constr += filpath;
                //建立连接
                connection = new OleDbConnection(constr);
                
                connection.Open();
                orderStr = "select * from student";
                oleAdapter = new OleDbDataAdapter(orderStr, connection);
                oleAdapter.Fill(dataSet, "student");
    
                int clms = dataSet.Tables["student"].Columns.Count;//表中多少行
                Console.WriteLine("{0}", clms);
    
                //遍历数据库
                String lineStr;
                for (int i = 0; i < clms; i++)
                {
                    lineStr = dataSet.Tables["student"].Rows[i]["stn"].ToString() + " " + dataSet.Tables["student"].Rows[i]["name"].ToString();
                    Console.WriteLine("{0}", lineStr);
                }
                //下面这两句话不加就会出现异常,并且更新、插入、删除无法实现
                cb = new OleDbCommandBuilder(oleAdapter);
                oleAdapter.UpdateCommand = cb.GetUpdateCommand();
    
                //插入一行
                /*DataRow dr = dataSet.Tables["student"].NewRow();
                dr["stn"] = 4;
                dr["name"] = "小强";
                dataSet.Tables["student"].Rows.Add(dr);
                oleAdapter.Update(dataSet, "student");*/
    
                //删除
    
                for (int i = 0; i < clms; i++)
                {
                    //lineStr = dataSet.Tables["student"].Rows[i]["stn"].ToString() + " " + dataSet.Tables["student"].Rows[i]["name"].ToString();
                    //Console.WriteLine("{0}", lineStr);
                    if (string.Equals("小强", dataSet.Tables["student"].Rows[i]["name"].ToString()))
                    { dataSet.Tables["student"].Rows[i].Delete(); oleAdapter.Update(dataSet, "student"); }
                }
                
                //dataSet.AcceptChanges();
    
                //cmd = new OleDbCommand("Delete from student where name = 小强", connection);
                //cmd.ExecuteNonQuery();
               
                connection.Close();
                    
                Console.ReadKey();
             
            }
    
    
        }
    }
    世上无难事,只要肯登攀。
  • 相关阅读:
    Lambda表达式
    java中解决小数精度问题
    [Unity]-黑魂复刻-动画 001
    kuka Virtual Remote pendant 连接使用
    C# 操作 pg 数据库
    C#常用字符串操作
    Go学习笔记之相关资源
    Go学习笔记之常用命令
    Go学习笔记之安装
    nginx学习笔记之安装
  • 原文地址:https://www.cnblogs.com/littlehoom/p/3681468.html
Copyright © 2011-2022 走看看