c# vs2010 excel 上传oracle数据
excel 數(shù)據(jù)表上傳到oracle數(shù)據(jù)庫。過程例如以下:
1、打開本地excel文件
2、用OleDb連接excel文件
3、將來excel的數(shù)據(jù)讀取到dataset中
4、把dataset 中數(shù)據(jù)insert到oracle中對(duì)應(yīng)的表中
以下截圖說明:
建立項(xiàng)目文件。非常easy。就是建立普通的winform項(xiàng)目。
當(dāng)中訪問oracle要加入引用System.Data.OracleClient;
vs2010 默認(rèn)是.net framework 4.0 client profile 。在加入引用時(shí)是看不到System.Data.OracleClient;須要在
項(xiàng)目文件上右擊。選擇屬性。會(huì)彈出例如以下對(duì)話框:
在target framework 下拉框中 選擇.net framework 4。這樣興許加入引用時(shí),才干在.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();//打開對(duì)話方塊
? ? ? ? ? ? this.textBox1.Text = openFileDialog1.FileName;//得到檔=路徑+名稱
? ? ? ? }
? ? ? ? private void button2_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? DataSet ds = ImportExcel(this.textBox1.Text);//將excel的對(duì)象先放到ds 中
? ? ? ? ? ? ? ? if (ds != null)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? if (ds.Tables[0].Rows.Count > 0)//假設(shè)ds中是有值的話 執(zhí)行以下的操作
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? if (ExportInfo(ds))
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? MessageBox.Show("導(dǎo)入資料庫成功!");
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? MessageBox.Show("導(dǎo)入資料庫失敗!");
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? catch
? ? ? ? ? ? {
? ? ? ? ? ? ? ? MessageBox.Show("導(dǎo)入資料庫失敗 請(qǐng)檢查導(dǎo)入檔是否填寫正確!");
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? 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,假設(shè)為其它excel版本號(hào)。請(qǐng)選擇對(duì)應(yīng)的連接驅(qū)動(dòng)和字符串
? ? ? ? ? ? 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)//假設(shè)ds中是有值的話 執(zhí)行以下的操作
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? return Do(ds);//執(zhí)行成功
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? return false;//執(zhí)行失敗
? ? ? ? }
? ? ? ? public static bool Do(DataSet ds)
? ? ? ? {
? ? ? ? ? ? OracleConnection conNorthwind = new OracleConnection("Data Source=tiptop;User Id=iteqdg;Password=iteqdg;Integrated Security=no;");//連結(jié)字串
? ? ? ? ? ? OracleCommand commandNorthwind = new OracleCommand();
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? conNorthwind.Open();//打開資料庫連結(jié)
? ? ? ? ? ? ? ? OracleTransaction tranNorthwind = conNorthwind.BeginTransaction();//開始事務(wù)
? ? ? ? ? ? ? ? for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? DataRow dr = ds.Tables[0].Rows[i];
? ? ? ? ? ? ? ? ? ? OracleParameter[] parameters = null;//為了得到插入資料庫的參數(shù) 定義參數(shù)物件 為空
? ? ? ? ? ? ? ? ? ? string sql = GetSqlString(dr, out parameters);//執(zhí)行sql -->用out關(guān)鍵字得到參數(shù) 賦到parameters物件上
? ? ? ? ? ? ? ? ? ? //插入資料庫中
? ? ? ? ? ? ? ? ? ? PrepareCommand(commandNorthwind, conNorthwind, tranNorthwind, sql, parameters);
? ? ? ? ? ? ? ? ? ? commandNorthwind.ExecuteNonQuery();//執(zhí)行操作
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? commandNorthwind.Transaction.Commit();//提交事務(wù)
? ? ? ? ? ? ? ? conNorthwind.Close();//關(guān)閉資料庫連結(jié)資源
? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? }
? ? ? ? ? ? catch//假設(shè)有異常 不一定要捕捉異常 但要rollback事務(wù)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (commandNorthwind.Transaction != null && conNorthwind != null)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? commandNorthwind.Transaction.Rollback();//rollback事務(wù)
? ? ? ? ? ? ? ? ? ? conNorthwind.Close();//關(guān)閉資料庫連結(jié)
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// 每一行資訊插入資料庫中
? ? ? ? /// </summary>
? ? ? ? /// <param name="dr">要插入的這一行ds-datarow對(duì)象</param>
? ? ? ? /// <returns>sql語句和用out關(guān)鍵字的參數(shù)陣列物件</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);
? ? ? ? }
? ? ? ? //參數(shù)設(shè)定 ?此方法被重載?
? ? ? ? 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);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
轉(zhuǎn)載于:https://www.cnblogs.com/llguanli/p/8919101.html
總結(jié)
以上是生活随笔為你收集整理的c# vs2010 excel 上传oracle数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 通信、计算机、电子相关专业技术工作
- 下一篇: iOS - 富文本