c# 连接oracle 读取数据

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;     } } 

转载于:https://www.cnblogs.com/naliang/p/oracle.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值