using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace PDM {
    public partial class BaseInfo : Form {
        public BaseInfo() {
            InitializeComponent();
            this.tbxFilePath.Text = "d:/TestPicture/ln/";
            this.hash.Text = "d:/TestPicture/ln/";
            this.server.Text = "PDMFiles/FMSC/";
        }
        List<SpareBaseInfo> SpareBaseInfos = null;
        List<SpareBaseInfo> SpareBaseInfoDataBases = null;
        List<SpareBaseInfo> SpareBaseInfoisexist= null;         StreamWriter sw = File.AppendText(Environment.CurrentDirectory + "\\SpareBaseInfoFile.txt");
      
        private void UpdateDataBase_Click(object sender, EventArgs e) {
            try {
                var updatesql = new StringBuilder();
                SpareBaseInfos = new List<SpareBaseInfo>();
                SpareBaseInfoDataBases = new List<SpareBaseInfo>();
                SpareBaseInfoisexist = new List<SpareBaseInfo>();
                foreach(var item in Directory.GetFiles(this.tbxFilePath.Text)) {
                    var extenname = Path.GetFileName(item);
                    var name = Path.GetFileNameWithoutExtension(item);
                    if(name.Contains("#")) {
                        string[] codename = name.Split('#');
                        SpareBaseInfo sbi = new SpareBaseInfo();
                        sbi.Code = codename[0].ToString();
                        sbi.Name = codename[1].ToString();
                        sbi.FilePath = this.server.Text + extenname;
                        if(!SpareBaseInfos.Contains(sbi)) {
                            SpareBaseInfos.Add(sbi);
                        }
                        updatesql.AppendLine("update SpareBaseInfo set FilePaths='" + server.Text + extenname + "'" + "where code='" + sbi.Code + "' and name= '" + sbi.Name + "';");
                        if(!File.Exists(this.hash.Text + extenname.GetHashCode())) {
                            Directory.CreateDirectory(this.hash.Text + Math.Abs(extenname.GetHashCode()) % 1000);
                        }
                        File.Copy(item, this.hash.Text + Math.Abs(extenname.GetHashCode()) % 1000 + "/" + extenname);
                    }
                } 
                sw.Write(updatesql.ToString());
                MessageBox.Show("成功");
                sw.Close();             } catch(Exception ex) {
                
                throw ex;
            }             string sql = "select * from SpareBaseInfo where code in (";
            var Codes = SpareBaseInfos.Select(l => l.Code);
            foreach(var item in Codes) {
                sql += "'" + item + "',";
            }
            if(!string.IsNullOrEmpty(sql)) {
                sql = sql.Remove(sql.Length - 1);
            }
            sql += ")";             sql += "and name in(";
            var Names = SpareBaseInfos.Select(l => l.Name);
            foreach(var item in Names) {
                sql += "'" + item + "',";
            }
            if(!string.IsNullOrEmpty(sql)) {
                sql = sql.Remove(sql.Length - 1);
            }
            sql += ")";
            string conn = "user id=mfepc;data source=" + server.Text + ";password=mfepc";
            OracleConnection cn = new OracleConnection(conn);
            cn.Open();//要打开连接   
            string strcmd = sql;
            OracleCommand cmd = new OracleCommand(strcmd, cn);
            OracleDataReader reader = cmd.ExecuteReader();
            try {
                while(reader.Read()) {
                    var a = reader[0].ToString();
                    SpareBaseInfo sbi = new SpareBaseInfo();
                    sbi.Id = Convert.ToInt32(reader["Id"].ToString());
                    sbi.Code = reader["Code"].ToString();
                    sbi.Name = reader["Name"].ToString();
                    if(!SpareBaseInfoDataBases.Contains(sbi)) {
                        SpareBaseInfoDataBases.Add(sbi);
                    }
                }
                dataGridView1.DataSource = SpareBaseInfoDataBases;
            } finally {
                reader.Close();
            }
            var updatesql = new StringBuilder();
            try {
                if(MessageBox.Show(SpareBaseInfoDataBases.Count() + "条数据是否继续", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes) {
                    updatesql.AppendLine("begin");
                    foreach(var item in SpareBaseInfoDataBases) {
                        item.FilePath = SpareBaseInfos.FirstOrDefault(l => l.Name == item.Name && l.Code == item.Code).FilePath;
                        updatesql.AppendLine("update SpareBaseInfo set FilePaths='" + item.FilePath + "'" + "where code='" + item.Code + "' and name= '" + item.Name + "';");
                    }
                    updatesql.AppendLine("end;");
                    //OracleCommand cmdupdate = new OracleCommand(updatesql.ToString(), cn);
                    //cmdupdate.ExecuteNonQuery();
                    MessageBox.Show("成功");
                }             } catch(Exception ecp) {
                throw ecp;
            }             cn.Close();
        }
    }     public class SpareBaseInfo {         private int id;         public int Id {
            get {
                return id;
            }
            set {
                id = value;
            }
        }         private string code;         public string Code {
            get {
                return code;
            }
            set {
                code = value;
            }
        }         private string name;         public string Name {
            get {
                return name;
            }
            set {
                name = value;
            }
        }         private string filePath;         public string FilePath {
            get {
                return filePath;
            }
            set {
                filePath = value;
            }
        }
        //通过DataSet来读取数据:         //创建和数据库的连接192.168.17.42/epcv2
        //OracleConnection oraCon = new OracleConnection("user id=mfepc0521;data source=192.168.17.42/mfdmstest;password=mfepc0521");
        //OracleConnection oraCon = new OracleConnection("user id=mfepc;data source=192.168.17.42/epcv2;password=mfepc");
        //OracleDataAdapter oraDap = new OracleDataAdapter("select * from SpareBaseInfo where rownum<10", oraCon);
        //DataSet ds = new DataSet();
        //oraDap.Fill(ds);
        //DataTable _table = ds.Tables[0];
        //int count = _table.Rows.Count;
        //dataGridView1.DataSource = _table;
    }
}

c# 连接oracle 读取数据的更多相关文章

  1. 连接oracle读取数据

    没怎么用过oracle,而且是在地税内网内部估计是防火墙的原因虚拟机里也连不上oracle,刚开始费了很多周折查找问题,现在又放弃使用直连数据库了,记下来以备后用吧 public class Load ...

  2. 简谈python从Oracle读取数据生成图形

    初次学习python,连接Oracle数据库,导出数据到Excel,再从Excel里面读取数据进行绘图,生成png保存出来. 1.涉及到的python模块(模块安装就不进行解释了): 1 import ...

  3. python连接oracle导出数据文件

    python连接oracle,感觉table_list文件内的表名,来卸载数据文件 主脚本: import os import logging import sys import configpars ...

  4. 水晶报表连接Oracle做数据报表笔记

    首先,新建一个水晶报表的文件,这个时候要给这个报表文件绑定一个oracle数据源, 选择右侧菜单的这个东西,选择“数据库专家”,打开之后是这么一个界面: 选择建立新连接: 这个地方最关键,也是我为什么 ...

  5. linux shell脚本连接oracle查询数据插入文件和日志文件中

    #!/bin/sh sqlplus "用户名/密码@数据库"<<EOF  或者只有一个库的 :sqlplus "用户名/密码"<<EOF ...

  6. C# Socket TcpClient 无法从传输连接中读取数据: 远程主机强迫关闭了一个现有的连接。。

    开始的代码: byte[] data = Encoding.UTF8.GetBytes(sInfo);                    tcpns.Write(data, 0,1024); 修改 ...

  7. EF6 连接Oracle 迁移数据错误解决方法

    环境:vs2015 + EF6 +ODP 数据库Oracle 11G add-migratioin 正常,但在update-database 时报如下错误: System.Runtime.Serial ...

  8. python3.4连接和读取oracle数据表

    想用python连接Oracle并查询数据表,就写了个Demo.参考了以下网址. Python学习之 cx_Oracle学习记录 一 http://my.oschina.net/bxxfighting ...

  9. C#连接Oracle数据库查询数据

    C#连接Oracle数据库可以实现许多我们需要的功能,下面介绍的是C#连接Oracle数据库查询数据的方法,如果您对C#连接Oracle数据库方面感兴趣的话,不妨一看. using System; u ...

随机推荐

  1. AppDomain

    AppDomain http://www.cnblogs.com/xiashengwang/archive/2012/07/17/2594623.html http://www.cnblogs.com ...

  2. 队爷的新书 CH Round #59 - OrzCC杯NOIP模拟赛day1

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的新书 题解:看到这题就想到了 poetize 的封 ...

  3. 纯css切换左侧菜单

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  4. 【转】iOS 开发怎么入门?

    原文网址:http://www.zhihu.com/question/20264108 iOS 开发怎么入门? 请问有设计模式.内存管理方面的资料吗?最好有除了官方文档之外的其它内容,10 条评论 分 ...

  5. 未能导入activex控件,请确保它正确注册"的完美解决方案

    这个错误“未能导入activex控件,请确保它正确注册”昨天下午让我和我同事花费了3个小时来调试这个错误,在使用VS2010的winfrom编程时加入com组件的时候,报这个错误(以一个客户的机器上) ...

  6. leetcode 合并区间

    使用最简单的排序方法: /** * Definition for an interval. * public class Interval { * int start; * int end; * In ...

  7. centos 6安装报错

    Missing ISO image The installer has tried to . but cannot find it on the hard drive. Please copy thi ...

  8. 《University Calculus》-chape8-无穷序列和无穷级数-基本极限恒等式

    基于基本的极限分析方法(诸多的无穷小以及洛必达法则),我们能够得到推导出一些表面上看不是那么显然的式子,这些极限恒等式往往会在其他的推导过程中用到,其中一个例子就是概率论中的极限定理那部分知识.

  9. lightoj1057 - Collecting Gold (tsp问题)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1057 题目大意:在二维矩阵中,给你一个起点和至多15个的目标点.要你求出从起点出发经过 ...

  10. crontab使用--linux下的定时任务程序

    crontab是一个linux下的定时运行程序,如果我们想让自己的程序定时执行, 可以把自己的程序交给这个程序来完成 第一步:配置crontab的脚本的默认编辑器,它的默认的编辑器不好用,我们配置vi ...