c# 连接oracle 读取数据
2015-08-15 13:19
591 查看
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; } }
相关文章推荐
- 检索06 - Oracle MySql SqlSever之间的区别和优缺点
- oracle用户创建及权限设置
- 浅说两种输出Oracle字符文件的方法
- Oracle触发器
- 收藏:oracle中比较两表表结构差异和数据差异的方法
- oracle学习笔记(六)
- Oracle逻辑存储结构1—概述
- Oracle优化:大量数据插入或更新
- oracle表连接——处理连接过程中另外一张表没有相关数据不显示问题
- oracle的正则表达式
- Oracle常用函数
- ORACLE常用数值函数、转换函数、字符串函数
- oracle存储过程常用技巧
- Oracle 中union的用法
- oracle 学习笔记(五)
- oracle学习笔记(四)
- 为何要把你的SAP运行在Oracle数据库上?
- 甲骨文称 Google 毁掉了 Java 的市场
- Oracle Regexp_substr
- Oracle_object_id和data_object_id的区别与联系