⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 才软件采用的是C#.NET平台开发
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//using Excel = Microsoft.Office.Interop.Excel;
using System.Data.OracleClient;
using System.Data.Odbc;
using System.Data.OleDb;
using System.IO;

namespace WindowsApplication2
{
    public partial class Form1 : Form
    {
        //string excelFilePath = @"D:\inetpub\Visual Studio 2005\UncertaintyReport\b.xls";
        //string dbfFilePath = @"D:\d.dbf";//生成dbf文件
        string tabledbf = "";   //读取dbf文件
        string tablename = "MCS_DBF";    //数据库表
        //string Connectionpath = "";
        string OracleConnectionString = "";

        public Form1()
        {
            InitializeComponent();
            TextReader tr = new StreamReader("ConConfig.ini");
            OracleConnectionString = tr.ReadLine().Trim();
            tr.Close();
            tr = null;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            tablename = table.Text;   //表名
            //tablename = "MCS_DEVICE_BASEINFO";
            tabledbf = dbfPath.Text;   //获得dbf文件路径
            //tabledbf = "D:\\导入备份\\whole\\1.dbf";
            //Connectionpath = conpath.Text; //连接字符串路径

            bool temp2 = true;
            int count = 4;
            int Columns = 0;  //取得列值
            DataSet ds = new DataSet();
            if (tablename != "" && tabledbf != "")
            {
                try
                {
                    OdbcConnection conn = new OdbcConnection();  //读dbf
                    string connStr = @"Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=" + tabledbf + ";Exclusive=No;NULL=NO;Collate=Machine;BACKGROUNDFETCH=NO;DELETED=NO";
                    conn.ConnectionString = connStr;
                    conn.Open();
                    string sql = "select * from " + tabledbf;
                    OdbcDataAdapter da = new OdbcDataAdapter(sql, conn);
                    da.Fill(ds);
                    dataGridView1.DataSource = ds.Tables[0].DefaultView;
                    
                    for (int i = 0; i < dataGridView1.ColumnCount; i++)
                    {
                        //int num = i + 1;
                        //string text = "n" + num;
                        //string a = ds.Tables[0].Rows[i].ToString();
                        //string b = dataGridView1[i, 0].Value.ToString();
                        if (dataGridView1[i, 0].Value.ToString().Trim() != "")
                        {
                            Columns++;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                catch(Exception ex)
                {
                    temp2 = false;
                    if (ex.Message == "ERROR [42000] [Microsoft][ODBC Visual FoxPro Driver]Command contains unrecognized phrase/keyword.")
                    {
                        MessageBox.Show("dbf文件不能在中文目录下转换!");
                    }
                }
                /* int Rows = 0;//取得行值
                 for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                 {
                     bool temp = false;
                     for (int j = 0; j < Columns; j++)
                     {
                         string a = dataGridView1[j, i].Value.ToString().Trim();
                         if (dataGridView1[j, i].Value.ToString().Trim() != "")
                         {
                             temp = true;
                         }
                     }
                     if (temp)
                     {
                         Rows++;
                     }
                     else
                     {
                         break;
                     }
                 }*/
                if (temp2)
                {
                    string bh = "";
                    for (int i = 1; i < dataGridView1.RowCount - 1; i++)   //写入数据库
                    {
                        try
                        {
                            string ziduan = "";
                            string data = "";
                            bh = dataGridView1[0, i].Value.ToString().TrimStart().TrimEnd();
                            for (int j = 0; j < Columns; j++)
                            {
                                string data1 = "'" + dataGridView1[j, i].Value.ToString().TrimStart().TrimEnd() + "',";
                                if (data1 == "'/',")
                                {
                                    data1 = "' ',";
                                }

                                if (dataGridView1[j, 0].Value.ToString().IndexOf("Date") != -1) //判断时间格式
                                {
                                    try
                                    {
                                        if (dataGridView1[j, i].Value.ToString().TrimStart().TrimEnd() != "/")
                                        {
                                            Convert.ToDateTime(dataGridView1[j, i].Value.ToString().TrimStart().TrimEnd());
                                            if (data1.IndexOf(' ') != -1)
                                            {
                                                data1 = "to_date('" + data1.Replace("'", "") + "','yyyy-mm-dd hh:mm:ss'),";
                                            }
                                            else
                                            {
                                                data1 = "to_date('" + data1.Trim().Replace("'", "").Replace(",", "") + "','yyyy-mm-dd'),";
                                            }
                                        }
                                        else
                                        {
                                            data1 = "to_date('','yyyy-mm-dd'),";
                                        }
                                    }
                                    catch
                                    {
                                        data1 = "''";
                                    }
                                }

                                if (dataGridView1[j, 0].Value.ToString().IndexOf("NUM") != -1)  //判断数字类型
                                {
                                    if (dataGridView1[j, i].Value.ToString().TrimStart().TrimEnd() != "/")
                                    {
                                        ziduan += ds.Tables[0].Columns[j].ToString().TrimStart().TrimEnd() + ",";
                                    }
                                    else
                                    {
                                        data1 = "";
                                    }
                                }
                                else
                                {
                                    ziduan += ds.Tables[0].Columns[j].ToString().TrimStart().TrimEnd() + ",";
                                }
                                data += data1;
                            }

                            ziduan = ziduan.Remove(ziduan.Length - 1, 1);
                            data = data.Remove(data.Length - 1, 1);
                            data = data.Replace("''", "'',").Replace("'',,", "'',");
                            string Sql = "insert into " + tablename + "(" + ziduan + ") values(" + data + ")";

                            OracleConnection con = new OracleConnection(OracleConnectionString);
                            con.Open();
                            OracleCommand cmd = new OracleCommand(Sql, con);
                            cmd.ExecuteNonQuery();
                            cmd = null;
                            con.Close();
                            con = null;
                            count++;
                        }
                        catch (Exception ex)
                        {
                            textBox1.Text += "写入第" + count.ToString() + "条失败,编号:" + bh + ",Err:" + ex.Message + "\r\n";
                        }
                    }
                    MessageBox.Show("导入完毕");
                }
            }
            else
            {
                MessageBox.Show("文件路径不能为空!");
            }
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            dbfPath.Text = openFileDialog1.FileName.ToString();
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -