在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例Windows系统编程 → txt导入到access数据库示例源码(txt2access)

txt导入到access数据库示例源码(txt2access)

Windows系统编程

下载此实例
  • 开发语言:C#
  • 实例大小:0.09M
  • 下载次数:5
  • 浏览次数:22
  • 发布时间:2018-01-21
  • 实例类别:Windows系统编程
  • 发 布 人:441258458
  • 所需积分:1
 相关标签: Access

实例介绍

【实例简介】txt2access1

【实例截图】txt2access1

from clipboard

【核心代码】txt2access1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;

namespace txttoaccess1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string str_FilePath = "D:\\2.txt";
        //string str_DaraSource = "D:\\test.accdb";
        String str_FolderPath = "D:\\text";

        private void button1_Click(object sender, EventArgs e)
        {
            string P_Connection = string.Format(//创建数据库连接字符串
                //"Provider=Microsoft.jet.OLEDB.4.0;Data Source=test.mdb;User Id=Admin");
                  "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=test.accdb;User Id=Admin");
            OleDbConnection P_OLEDBConnection = //创建连接对象
            new OleDbConnection(P_Connection);
            P_OLEDBConnection.Open();//连接到数据库
            OleDbCommand P_OLEDBCommand = new OleDbCommand(//创建命令对象
                "select * from [book]",
                P_OLEDBConnection);

            DataTable dt = new DataTable();
            dt.Columns.Add("a");
            dt.Columns.Add("b");
            dt.Columns.Add("c");
            dt.Columns.Add("d");
            FileStream fs = new FileStream(str_FilePath, FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default);
            string strLine = sr.ReadLine();

            while (strLine != null)
            {
                string[] strArray = new string[4];
                strArray = strLine.Split('\t');
                DataRow dr = dt.NewRow();
                dr[0] = strArray[0];
                dr[1] = strArray[1];
                dr[2] = strArray[2];
                dr[3] = strArray[3];

                //String SQL = "select * from [book] where [1] Like '%"   dr[0]   "%'";
                 String SQL = "select * from [book] where [1]='" dr[0] "'";
                OleDbCommand cmd = new OleDbCommand(SQL, P_OLEDBConnection);
                DataTable dt1 = new DataTable();
                OleDbDataAdapter Adapter = new OleDbDataAdapter(cmd);
                Adapter.Fill(dt1);
                if (dt1.Rows.Count > 0)
                {
                    MessageBox.Show(dr[0] "已存在,请修改文件名后导入");
                    dt.Clear();
                    return ;
                }
                else
                {
                    dt.Rows.Add(dr);
                    strLine = sr.ReadLine();
                }
            }

            for (int i = 0; i < dt.Rows.Count; i  )
            {
                string sql = "insert into book values('"   dt.Rows[i][0]   "','"   dt.Rows[i][1]   "','"   dt.Rows[i][2]   "','"   dt.Rows[i][3]   "')";
                P_OLEDBCommand.CommandText = sql;
                P_OLEDBCommand.ExecuteNonQuery();
            }
            MessageBox.Show( "导入数据成功!");
            sr.Close();
            fs.Close();
            P_OLEDBConnection.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string P_Connection = string.Format(//创建数据库连接字符串
                //"Provider=Microsoft.jet.OLEDB.4.0;Data Source=test.mdb;User Id=Admin");
                  "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=test.accdb;User Id=Admin");
            OleDbConnection P_OLEDBConnection = //创建连接对象
            new OleDbConnection(P_Connection);
            P_OLEDBConnection.Open();//连接到数据库
            OleDbCommand P_OLEDBCommand = new OleDbCommand(//创建命令对象
                "select * from [book]",
                P_OLEDBConnection);
            DataTable dt1 = new DataTable();
            foreach (String fileName in Directory.GetDirectories(str_FolderPath))  //GetDirectories获取文件夹列表
            {
                FileInfo file = new FileInfo(fileName);//声明一个操作文件的实例
                //String SQL = "select * from [book] where [1] Like '%"   file.Name   "%'";
                 String SQL = "select * from [book] where [1]='" file.Name "'";
                OleDbCommand cmd = new OleDbCommand(SQL, P_OLEDBConnection);
                OleDbDataAdapter Adapter = new OleDbDataAdapter(cmd);
                Adapter.Fill(dt1);
            }
            P_OLEDBConnection.Close();
            if (dt1.Rows.Count > 0)
            {
                for (int i = 0; i < dt1.Rows.Count; i  )
                {
                    Directory.Move(str_FolderPath   "\\"   dt1.Rows[i][0], str_FolderPath   "\\"   dt1.Rows[i][0]   dt1.Rows[i][1]   dt1.Rows[i][2]   dt1.Rows[i][3]);
                }
                MessageBox.Show("修改成功!");
            }
            else
                MessageBox.Show("未找到要修改文件夹!");
        }

        public static string ChangeFilesNames(string str_oldname)
        {
            string P_Connection = string.Format(//创建数据库连接字符串
                //"Provider=Microsoft.jet.OLEDB.4.0;Data Source=test.mdb;User Id=Admin");
                  "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=test.accdb;User Id=Admin");
            OleDbConnection P_OLEDBConnection = //创建连接对象
            new OleDbConnection(P_Connection);
            P_OLEDBConnection.Open();//连接到数据库
            OleDbCommand P_OLEDBCommand = new OleDbCommand(//创建命令对象
                "select * from [book]",
                P_OLEDBConnection);
                //String SQL = "select * from [book] where [1] Like '%"   str_oldname   "%'";
                String SQL = "select * from [book] where [1]='" str_oldname "'";
                OleDbCommand cmd = new OleDbCommand(SQL, P_OLEDBConnection);
                DataTable dt1 = new DataTable();
                OleDbDataAdapter Adapter = new OleDbDataAdapter(cmd);
                Adapter.Fill(dt1);
            P_OLEDBConnection.Close();
                string str_changedname="";
                if (dt1.Rows.Count > 0)
                {
                    str_changedname=str_oldname   dt1.Rows[0][1]   dt1.Rows[0][2]   dt1.Rows[0][3];
                }
                else
                    MessageBox.Show("未找到该目录!");
                if (str_changedname.Length > 248)
                    return str_changedname.Substring(0, 247);
                else 
                    return str_changedname;
            }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox2.Text = ChangeFilesNames(textBox1.Text);
        }

        private void button4_Click(object sender, EventArgs e)
        {

            try
            {
                string P_Connection = string.Format(//创建数据库连接字符串
                    //"Provider=Microsoft.jet.OLEDB.4.0;Data Source=test.mdb;User Id=Admin");
                  "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=test.accdb;User Id=Admin");
                OleDbConnection P_OLEDBConnection = //创建连接对象
                    new OleDbConnection(P_Connection);
                P_OLEDBConnection.Open();//连接到数据库
                OleDbCommand P_OLEDBCommand = new OleDbCommand(//创建命令对象
                    "select * from [book]",
                    P_OLEDBConnection);
                    string cmdString = "delete from [book] where [1]= '"   textBox3.Text   "'";
                    //注意where处没有单引
                    P_OLEDBCommand.CommandText = cmdString;
                    P_OLEDBCommand.ExecuteNonQuery();
                    P_OLEDBConnection.Close();
                MessageBox.Show(//弹出消息对话框
                   "数据删除成功!\r\n", "完成!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(//弹出消息对话框
                    "数据删除失败!\r\n"   ex.Message, "错误!");
            }
        }

        private void button5_Click(object sender, EventArgs e)
        { try
            {
            string P_Connection = string.Format(//创建数据库连接字符串
                //"Provider=Microsoft.jet.OLEDB.4.0;Data Source=test.mdb;User Id=Admin");
              "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=test.accdb;User Id=Admin");
            OleDbConnection P_OLEDBConnection = //创建连接对象
                new OleDbConnection(P_Connection);
            P_OLEDBConnection.Open();//连接到数据库
            OleDbCommand P_OLEDBCommand = new OleDbCommand(//创建命令对象
                "select * from [book]",
                P_OLEDBConnection);
            string RefreshString = "update [book] set [2]='"   textBox4.Text   "' where [1]='"   textBox5.Text "'";
            P_OLEDBCommand.CommandText = RefreshString;
            P_OLEDBCommand.ExecuteNonQuery();
            P_OLEDBConnection.Close();
            MessageBox.Show(//弹出消息对话框
               "数据修改成功!\r\n", "完成!");
            }
        catch (Exception ex)
        {
            MessageBox.Show(//弹出消息对话框
                "数据删除失败!\r\n"   ex.Message, "错误!");
        }
            
        }


        








    }
}

标签: Access

实例下载地址

txt导入到access数据库示例源码(txt2access)

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
;
报警
var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");document.write(unescape("%3Cspan id='cnzz_stat_icon_4847253'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s13.cnzz.com/stat.php%3Fid%3D4847253' type='text/javascript'%3E%3C/script%3E"));