1. 程式人生 > >將表匯出資料庫成.txt檔案 將.txt檔案匯入資料庫成表

將表匯出資料庫成.txt檔案 將.txt檔案匯入資料庫成表

using System.Data.SqlClient;
using System.IO;
using System.Configuration;

namespace 錶轉txt txt轉表
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private static string path = ConfigurationManager.AppSettings["path"].ToString().Trim();
        private
static string daochu = ConfigurationManager.AppSettings["daochu"].ToString().Trim(); private static string daoru = ConfigurationManager.AppSettings["daoru"].ToString().Trim(); private void Form1_Load(object sender, EventArgs e) { #region 將資料庫中的表匯出成.txt格式的檔案 string
txt = ""; SqlConnection sqlcon = new SqlConnection(daochu); sqlcon.Open(); string tt = "select * from Permissions"; SqlDataAdapter sda = new SqlDataAdapter(tt, sqlcon); DataTable dt = new DataTable(); try { sda.Fill(dt); if
(dt.Rows.Count > 0) { for (int i = 0; i < dt.Rows.Count; i++) { for (int j = 0; j < dt.Columns.Count; j++) { if (j == (dt.Columns.Count - 1)) { txt += "" + dt.Rows[i][j].ToString().Trim() + ""; } else { txt += "" + dt.Rows[i][j].ToString().Trim() + "|"; } } Login(txt); txt = ""; } } MessageBox.Show("成功匯出檔案"); } catch (Exception ex) { MessageBox.Show("未能成功匯出檔案"); } sqlcon.Close(); dt.Dispose(); #endregion Reader(); } public void Reader() { #region 將.txt格式檔案匯入成資料庫中的表 SqlConnection conn = new SqlConnection(daoru); conn.Open(); string se = "if not exists(select * from sysobjects where id=object_Id('HumanInformation.Permissions') )CREATE TABLE Permissions(PermissionsId VarChar(15),PermissionsName VarChar(15)) "; SqlCommand sa = new SqlCommand(se, conn); try { sa.ExecuteNonQuery(); } catch (System.Data.SqlClient.SqlException ex) { Console.WriteLine(ex.Message); } conn.Close(); Set(); #endregion } public void Login(string txt) { string path = @"" + Form1.path + ""; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string fp = path + "Permissions.txt"; StringBuilder str = new StringBuilder(); str.Append("" + txt + ""); StreamWriter sw; if (!File.Exists(fp)) { sw = File.CreateText(fp); } else { sw = File.AppendText(fp); } sw.WriteLine(str.ToString()); sw.Close(); } public void Set() { StreamReader sr = new StreamReader(new FileStream("" + path + "Permissions.txt", FileMode.Open, FileAccess.Read), System.Text.Encoding.Default); SqlConnection cn = new SqlConnection(daoru); string insertCmd = "insert into Permissions values (@id, @name)"; char[] delimChar = new char[] { '|' }; string[] split = null; string strTemp = sr.ReadLine(); while (strTemp != null) { split = strTemp.Split(delimChar); SqlCommand cmd = new SqlCommand(insertCmd, cn); cmd.Parameters.Add("@id", SqlDbType.VarChar).Value = split[0]; cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = split[1]; cn.Open(); try { cmd.ExecuteNonQuery(); } catch (System.Data.SqlClient.SqlException ex) { Console.WriteLine(ex.Message); } cn.Close(); strTemp = sr.ReadLine(); } MessageBox.Show("成功匯入資料庫"); sr.Close(); } } }

相關推薦

使用kettle文字檔案中的資料匯入資料庫

1.準備好如下資料(以|鍵分割) JY01011604280016|JY010116042800160019|1|1|2017-04-27|1000000.00|121666.66|1000000.00|121666.66| |01 |

匯出資料庫.txt檔案 .txt檔案匯入資料庫

using System.Data.SqlClient; using System.IO; using System.Configuration; namespace 錶轉txt txt轉表 { public partial class Form1 :

mysql匯入資料load data infile用法(txt檔案中的資料匯入中)

我們常常匯入資料!mysql有一個高效匯入方法,那就是load data infile 下面來看案例說明   基本語法: load data  [low_priority] [local] infile 'file_name txt' [replace | ignor

eclipse程式匯出jar檔案

使用eclipse軟體開發,對於成品經常需要將程式碼打成jar包,然後將jar包拷貝到伺服器進行執行。使用eclipse打包的過程也比較簡單,但是也有很多坑,一不小心就會入坑。這篇部落格介紹如何在eclipse中將工程java程式碼(單獨的java程式碼也可)打成jar包。 工具/原料 電腦

mysql資料庫字符集的修改以及的字符集的修改,以及*.sql檔案匯入資料庫

一、我們需要將預設的字符集更改為utf-8來解決資料庫匯入資料的亂碼問題。 我們需要先檢視資料庫預設的字符集是什麼: show variables like 'character_set_%' 檢視所有的預設字符集 mysql> show variables like 'cha

關於使用java程式碼資料庫裡的匯出到Excel中

package CurriculumDesign; import java.io.FileOutputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatemen

SAP ABAP資料匯出TXT和從TXT讀取到內

*&---------------------------------------------------------------------**& Report  ZCMH0010*&*&--------------------------------------------

關於Java Web 使用 POI 資料庫 匯出 Excel 的完整例項

//匯出Excel  public void exportExcel() throws InterruptedException, IOException{   HSSFWorkbook hwb = new HSSFWorkbook();//第一步,建立一個workbook

mysql 資料匯出excel檔案(.xls格式)

匯出為txt檔案: select * from xi_table into outfile ’d:\test.txt’; 有時候有excel開啟的時候會出現亂碼, 因為office預設的是gb2312編碼,伺服器端生成的很有可能是utf-8編碼,這個時候你有兩種選擇: 在伺服器端使用iconv來進行編碼轉換:

如何mysql資料庫匯出到本地csv格式

1.MySQL匯出本地資料庫資料到本地檔案 mysql -A service_db -h your_host -utest -ptest mysql> select * from t_apps where created>'2012-07-02 00:00

Excel或者Txt檔案中的資料匯入Oracle資料庫

使用SQL*Loader前提必須oracle資料中目的表已經存在大體步驟如下:     1   將excle檔案另存為一個新檔案比如檔名為text.txt,檔案型別選文字檔案(製表符分隔),這裡選擇型別為csv(逗號分隔)也行,但是在寫後面的control.ctl時要將欄位終

Java中用poi技術報表匯出excel

    我相信有很多程式猿都像小M一樣遇到過需要將報表匯出成excel表的情況,看了網上的demo還是有點蒙的人,看過來,在這裡由小M系統的給大家講解一下。    在這裡,小M用的技術是poi技術,框架是springBoot+Maven,有需要的參考下(1)、首先,先引入ma

mysql命令列下資料匯出excel檔案(.xls格式)

在mysql命令列下執行以下sql語句: mysql>select * from xi_table into outfile ’/tmp/test.xls’; 一句話搞定。 linux系統儘量匯出在/tmp目錄下,不會涉及讀寫許可權問題! 匯出為txt檔案: se

利用POI資料庫匯出到Excel

在一個石化的專案中遇到了一個要求,需要將資料庫的一個表的資料導到Excel中,我也是剛出來工作不久,對這些開源的東西還不是很熟悉。後來認真看了一下,借這個機會把自己的一點理解寫出來,一來用於鞏固自己的學習,二來給別人一些參考吧! Apache POI是Apac

###好好好####Tensorflow模型匯出為一個檔案及介面設定

    在用PMML實現機器學習模型的跨平臺上線中,我們討論了使用PMML檔案來實現跨平臺模型上線的方法,這個方法當然也適用於tensorflow生成的模型,但是由於tensorflow模型往往較大,使用無法優化的PMML檔案大多數時候很笨拙,因此本文我們專門討論下tensorflow機器學習模型的

使用php語句資料庫*.sql檔案匯入資料庫

最簡單的php語句把資料庫*.sql檔案匯入資料庫  複製程式碼 程式碼如下: $sql=file_get_contents("text.sql"); //把SQL語句以字串讀入$sql  $a=explode(";",$sql); //用explode()函

PHPExcelexcel的資料匯入資料庫

一:話不多說,上乾貨     1.首先,需要建立個數據庫 product     資料庫程式碼參考: /* Navicat MySQL Data Transfer Source Server : TP-jifen-demo

使用PL SQL資料匯出為Excel格式檔案

使用PL SQL將資料匯出為Excel格式檔案有兩種方法,第一種是先將查詢結果匯出為CSV檔案,然後再轉為Excel檔案;第二種是選中要匯出的查詢結果,右鍵,選擇複製到xls,即可。 兩種方法各有優勢: 第一種方法適用於匯出資料量特別大,如超過140多萬行資料,因為excel表格有最大行數限

Pythontxt檔案變為excel檔案

首選展示一下txt檔案 轉變為excel檔案如下: 具體程式碼如下: txtname = 'baifenbi1.txt' excelname = '2.xlsx' fopen = open(txtname, 'r') lines = fopen.readlines()

pythonexcel檔案變成txt檔案

import xlrd def row2str(row_data): values = ""; for i in range(len(row_data)): if i == len(row_data) - 1: values = val