zoukankan      html  css  js  c++  java
  • 文件导入数据库

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using static System.Console;
    
    namespace 文件导入
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
    
            private void selectFileButton_Click(object sender, EventArgs e)
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "文本文件|*.txt";
                if (ofd.ShowDialog() == DialogResult.OK)
                { 
                    this.textBoxPath.Text = ofd.FileName;
                    //导入数据工作
                    ImportData(ofd.FileName);
    
    
                }
               
    
    
            }
    
            private void ImportData(string fileName)
            {
                /*var strs = File.ReadLines(fileName);
    
                foreach(var str in strs)
                {
                    WriteLine($"{str}");
                }*/
                string temp = string.Empty;
                using(StreamReader reader = new StreamReader(fileName,Encoding.UTF8))
                {
                    reader.ReadLine();
                    string connStr =
                            "server=.\SQLEXPRESS;uid=sa;pwd=luohanhui2016;database=StudentsInfo";
    
                    using (SqlConnection conn = new SqlConnection(connStr))
                    {
                        using (SqlCommand cmd = conn.CreateCommand())
                        {
                            conn.Open();
    
    
                            while (!string.IsNullOrEmpty(temp = reader.ReadLine()))
                           {
                        //WriteLine(temp);拿到了数据流
                        var strs = temp.Split(' ');
    
                        //拼接脚本
    
                        string sql = string.Format(@"insert into tblStudent
    (stuName,stuSex,stuBirthDate,stuPhone)values('{0}','{1}','{2}','{3}')", strs[1], strs[2],strs[3], strs[4]);
    
                        
    
    
                                
                                cmd.CommandText = sql;
                                cmd.ExecuteNonQuery();
    
                            }
    
                         }
                    }
                    MessageBox.Show("文件导入成功!");
    
                }
            }
        }
    }
  • 相关阅读:
    java-工具代码
    idea-常用快捷键
    idea-环境配置
    mysql-常用命令
    Java IO流学习总结
    Java类加载机制
    Struts2标签 %{ } %{# }详解
    EL语法
    SQL语句
    在servlet转向jsp页面的路径问题
  • 原文地址:https://www.cnblogs.com/Mr-Prince/p/12177477.html
Copyright © 2011-2022 走看看