c# vs2010 excel 上传oracle数据
excel 數據表上傳到oracle數據庫。過程例如以下:
1、打開本地excel文件
2、用OleDb連接excel文件
3、將來excel的數據讀取到dataset中
4、把dataset 中數據insert到oracle中對應的表中
以下截圖說明:
建立項目文件。非常easy。就是建立普通的winform項目。
當中訪問oracle要加入引用System.Data.OracleClient;
vs2010 默認是.net framework 4.0 client profile 。在加入引用時是看不到System.Data.OracleClient;須要在
項目文件上右擊。選擇屬性。會彈出例如以下對話框:
在target framework 下拉框中 選擇.net framework 4。這樣興許加入引用時,才干在.net頁簽看到System.Data.OracleClient。
以下是所有代碼
using System;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.OleDb;
using System.Data.OracleClient;
namespace WindowsFormsApplication4
{
? ? public partial class Form1 : Form
? ? {
? ? ? ? public Form1()
? ? ? ? {
? ? ? ? ? ? InitializeComponent();
? ? ? ? }
? ? ? ? private void button1_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? openFileDialog1.Filter = "1(*.xlsx)|*.xlsx";
? ? ? ? ? ? openFileDialog1.ShowDialog();//打開對話方塊
? ? ? ? ? ? this.textBox1.Text = openFileDialog1.FileName;//得到檔=路徑+名稱
? ? ? ? }
? ? ? ? private void button2_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? DataSet ds = ImportExcel(this.textBox1.Text);//將excel的對象先放到ds 中
? ? ? ? ? ? ? ? if (ds != null)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? if (ds.Tables[0].Rows.Count > 0)//假設ds中是有值的話 執行以下的操作
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? if (ExportInfo(ds))
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? MessageBox.Show("導入資料庫成功!");
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? MessageBox.Show("導入資料庫失敗!");
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? catch
? ? ? ? ? ? {
? ? ? ? ? ? ? ? MessageBox.Show("導入資料庫失敗 請檢查導入檔是否填寫正確!");
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? public static DataSet ImportExcel(string file)
? ? ? ? {
? ? ? ? ? ? FileInfo fileInfo = new FileInfo(file);
? ? ? ? ? ? if (!fileInfo.Exists) return null; string strConn = @"Provider=Microsoft.Ace.OleDb.12.0;Data Source=" + file + ";Extended Properties='Excel 12.0; HDR=yes; IMEX=2'";
? ? ? ? ? ?// 此處用的是excel2010,假設為其它excel版本號。請選擇對應的連接驅動和字符串
? ? ? ? ? ? OleDbConnection objConn = new OleDbConnection(strConn);
? ? ? ? ? ? DataSet dsExcel = new DataSet();
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? objConn.Open();
? ? ? ? ? ? ? ? string strSql = "select * from [Sheet1$]";
? ? ? ? ? ? ? ? OleDbDataAdapter odbcExcelDataAdapter = new OleDbDataAdapter(strSql, objConn);
? ? ? ? ? ? ? ? odbcExcelDataAdapter.Fill(dsExcel); return dsExcel;
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? throw ex;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? public static bool ExportInfo(DataSet ds)
? ? ? ? {
? ? ? ? ? ? if (ds != null)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (ds.Tables[0].Rows.Count > 0)//假設ds中是有值的話 執行以下的操作
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? return Do(ds);//執行成功
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? return false;//執行失敗
? ? ? ? }
? ? ? ? public static bool Do(DataSet ds)
? ? ? ? {
? ? ? ? ? ? OracleConnection conNorthwind = new OracleConnection("Data Source=tiptop;User Id=iteqdg;Password=iteqdg;Integrated Security=no;");//連結字串
? ? ? ? ? ? OracleCommand commandNorthwind = new OracleCommand();
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? conNorthwind.Open();//打開資料庫連結
? ? ? ? ? ? ? ? OracleTransaction tranNorthwind = conNorthwind.BeginTransaction();//開始事務
? ? ? ? ? ? ? ? for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? DataRow dr = ds.Tables[0].Rows[i];
? ? ? ? ? ? ? ? ? ? OracleParameter[] parameters = null;//為了得到插入資料庫的參數 定義參數物件 為空
? ? ? ? ? ? ? ? ? ? string sql = GetSqlString(dr, out parameters);//執行sql -->用out關鍵字得到參數 賦到parameters物件上
? ? ? ? ? ? ? ? ? ? //插入資料庫中
? ? ? ? ? ? ? ? ? ? PrepareCommand(commandNorthwind, conNorthwind, tranNorthwind, sql, parameters);
? ? ? ? ? ? ? ? ? ? commandNorthwind.ExecuteNonQuery();//執行操作
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? commandNorthwind.Transaction.Commit();//提交事務
? ? ? ? ? ? ? ? conNorthwind.Close();//關閉資料庫連結資源
? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? }
? ? ? ? ? ? catch//假設有異常 不一定要捕捉異常 但要rollback事務
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (commandNorthwind.Transaction != null && conNorthwind != null)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? commandNorthwind.Transaction.Rollback();//rollback事務
? ? ? ? ? ? ? ? ? ? conNorthwind.Close();//關閉資料庫連結
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// 每一行資訊插入資料庫中
? ? ? ? /// </summary>
? ? ? ? /// <param name="dr">要插入的這一行ds-datarow對象</param>
? ? ? ? /// <returns>sql語句和用out關鍵字的參數陣列物件</returns>
? ? ? ? public static string GetSqlString(DataRow dr, out OracleParameter[] parameters)
? ? ? ? {
? ? ? ? ? ? StringBuilder sb = new StringBuilder();
? ? ? ? ? ? sb.Append("INSERT INTO TEXT VALUES(:ID,:NAME)");
? ? ? ? ? ? parameters = new OracleParameter[] { new OracleParameter(":ID", Convert.ToString(dr[0])), new OracleParameter(":NAME", Convert.ToString(dr[1])) };
? ? ? ? ? ? return sb.ToString();//將sqlreturn出去
? ? ? ? }
? ? ? ? private static void PrepareCommand(OracleCommand cmd, OracleConnection conn, OracleTransaction trans, string cmdText, OracleParameter[] cmdParms)
? ? ? ? {
? ? ? ? ? ? PrepareCommand(cmd, conn, trans, cmdText, CommandType.Text, cmdParms);
? ? ? ? }
? ? ? ? //參數設定 ?此方法被重載?
? ? ? ? private static void PrepareCommand(OracleCommand cmd, OracleConnection conn, OracleTransaction trans, string cmdText, CommandType cmdType, OracleParameter[] cmdParms)
? ? ? ? {
? ? ? ? ? ? if (conn.State != ConnectionState.Open)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? conn.Open();
? ? ? ? ? ? }
? ? ? ? ? ? cmd.Connection = conn;
? ? ? ? ? ? cmd.CommandText = cmdText;
? ? ? ? ? ? if (trans != null)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? cmd.Transaction = trans;
? ? ? ? ? ? }
? ? ? ? ? ? cmd.CommandType = cmdType; ?// CommandType.Text;//cmdType;
? ? ? ? ? ? if (cmdParms != null)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? foreach (OracleParameter parameter in cmdParms)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? if (parameter != null)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? if (parameter.Value == null)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? parameter.Value = DBNull.Value;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? cmd.Parameters.Add(parameter);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
轉載于:https://www.cnblogs.com/llguanli/p/8919101.html
總結
以上是生活随笔為你收集整理的c# vs2010 excel 上传oracle数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 通信、计算机、电子相关专业技术工作
- 下一篇: C#调用带结构体指针的C Dll的方法