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();
             
            }
    
    
        }
    }
    世上无难事,只要肯登攀。
  • 相关阅读:
    poj 3243 Clever Y(BabyStep GiantStep)
    poj 2417 Discrete Logging
    poj 3481 Double Queue
    hdu 4046 Panda
    hdu 2896 病毒侵袭
    poj 1442 Black Box
    hdu 2815 Mod Tree
    hdu 3065 病毒侵袭持续中
    hdu 1576 A/B
    所有控件
  • 原文地址:https://www.cnblogs.com/littlehoom/p/3681468.html
Copyright © 2011-2022 走看看