110,571
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OracleClient;
using System.Data.OleDb;
using System.Data.Odbc;
using System.IO;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static DataSet ImportExcel(string file)
{
System.IO.FileInfo fileInfo = new FileInfo(file);
if (!fileInfo.Exists)
return null;
string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + file + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1'";
OleDbConnection objConn = new OleDbConnection(strConn);
DataSet dsExcel = new DataSet();
try
{
objConn.Open();
string strSql = "select * from [Sheet3$]";
OleDbDataAdapter odbcExcelDataAdapter = new OleDbDataAdapter(strSql, objConn);
odbcExcelDataAdapter.Fill(dsExcel);
return dsExcel;
}
catch (Exception ex)
{
throw ex;
}
finally
{
objConn.Close();
}
}
private void button4_Click(object sender, EventArgs e)
{
DataSet ds;
OpenFileDialog openFile = new OpenFileDialog();
openFile.Multiselect = false;
openFile.RestoreDirectory = true;
openFile.Filter = "Text files (*.xls)|*.xls|All files (*.*)|*.*";
if (openFile.ShowDialog() == DialogResult.OK)
{
textBox1.Text = openFile.FileName;
ds = ImportExcel(openFile.FileName);//获得Excel
}
else
{
return;
}
int odr = 0;
OracleConnection conn = new OracleConnection("Data Source=wzinner;Persist Security Info=True;User ID=common;password=common;Unicode=True"); //获得conn连接
try
{
conn.Open();
string queryString = "insert into em_transferss_t (transfers_id,transfers_OLDLANDNUM) values (:A,:B)";
OracleCommand com = new OracleCommand(queryString, conn);
int dsLength = ds.Tables[0].Rows.Count;// 获得Execle长度
for (int i = 0; i < dsLength; i++)
{
com.Parameters.Add("A", OracleType.Int32).Value = ds.Tables[0].Rows[i][0];
com.Parameters.Add("B", OracleType.VarChar).Value = ds.Tables[0].Rows[i][1];
odr = com.ExecuteNonQuery();//提交
}
conn.Close();
if (odr > 0)
{
MessageBox.Show("成功!");
if (MessageBox.Show("确认要更新吗??", "", MessageBoxButtons.OKCancel) != DialogResult.OK)
{
conn.Open();
int co = 0;
string sql = "delete from em_transferss_t";
OracleCommand comm = new OracleCommand(sql, conn);
co = comm.ExecuteNonQuery();
if (co > 0)
{
MessageBox.Show("数据未上传");
}
}
else
{
conn.Open();
int a = 0;
string updatesql = "update (select/*+ BYPASS_UJVC */ em_transfers_t.TRANSFERS_OLDLANDNUM a1,em_transfers_t.TRANSFERS_ID b1,em_transferss_t.TRANSFERS_OLDLANDNUM a2,em_transferss_t.TRANSFERS_ID b2" +
" from em_transfers_t,em_transferss_t) set a1=a2 where b1 =b2";
OracleCommand commm = new OracleCommand(updatesql, conn);
a = commm.ExecuteNonQuery();
if (a > 0)
{
MessageBox.Show("数据上传成功!");
}
}
}
}
catch (Exception ee)
{
MessageBox.Show(ee.Message.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
conn.Close();
}
}
}
}
conn.Open();
string queryString = "insert into transferss_t "+ "(transfers_id,transfers_OLDLANDNUM) values ({0},'{1}')";
OracleCommand com = conn.CreateCommand();
int dsLength = ds.Tables[0].Rows.Count;// 获得Execle长度
for (int i = 0; i < dsLength; i++)
{
queryString = string.Format(queryString,
ds.Tables[0].Rows[0][0],
ds.Tables[0].Rows[0][1]);
com.CommandText = queryString;
odr = com.ExecuteNonQuery();//提交
MessageBox.Show(queryString);
}
conn.Close();
//前面省略
using(OracleConnection conn = new OracleConnection("Data Source=wzinner;Persist Security Info=True;User ID=common;password=common;Unicode=True"))
{
try
{
conn.Open();//只要打开,不用关闭,使用了using会自动关闭的。
string queryString = "insert into em_transferss_t(transfers_id,transfers_OLDLANDNUM)values (:A,:B)";
using(OracleCommand com = new OracleCommand(queryString, conn))
{
int dsLength = ds.Tables[0].Rows.Count;// 获得Execle长度
com.Parameters.Add("A", OracleType.Int32);
com.Parameters.Add("B", OracleType.VarChar);
for (int i = 0; i < dsLength; i++)
{
com.Parameters["A"].Value = ds.Tables[0].Rows[i][0];
com.Parameters["B"].Value = ds.Tables[0].Rows[i][1];
odr = com.ExecuteNonQuery();//提交
}
}
if (odr > 0)
{
//省略
}
}
catch (Exception ee)
{
MessageBox.Show(ee.Message.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Excel.Application xApp=new Excel.ApplicationClass();
xApp.Visible=false;
//得到WorkBook对象, 可以用两种方式之一: 下面的是打开已有的文件
Excel.Workbook xBook=xApp.Workbooks._Open(fileName,
Missing.Value,Missing.Value,Missing.Value,Missing.Value
,Missing.Value,Missing.Value,Missing.Value,Missing.Value
,Missing.Value,Missing.Value,Missing.Value,Missing.Value);
Excel.Worksheet xSheet=(Excel.Worksheet)xBook.Sheets[1];
//取得行数
int iRowsCount=xSheet.UsedRange.Cells.Rows.Count;
//取得列数
int iColsCount=xSheet.UsedRange.Cells.Columns.Count;