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();
             
            }
    
    
        }
    }
    世上无难事,只要肯登攀。
  • 相关阅读:
    Bootstrap3 代码-代码块
    DB2 911错误的解释
    Bootstrap3 代码-用户输入
    Bootstrap3 代码-内联代码
    Bootstrap3 排版-列表
    Bootstrap3 排版-引用
    Bootstrap3 排版-地址
    Bootstrap3 排版-缩略语
    Bootstrap3 排版-改变大小写
    Bootstrap3 排版-对齐
  • 原文地址:https://www.cnblogs.com/littlehoom/p/3681468.html
Copyright © 2011-2022 走看看