zoukankan      html  css  js  c++  java
  • 数据导入与导出


    namespace 导入导出数据
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }


            private void button3_Click(object sender, EventArgs e)
            {
                this.Close();
            }


            private void button1_Click(object sender, EventArgs e)
            {
                if (openFileDialog1.ShowDialog() != DialogResult.OK)
                { return; }
                else
                {
                    using (FileStream fileStream = File.OpenRead(openFileDialog1.FileName))
                    {
                        using (StreamReader streamReader = new StreamReader(fileStream))
                        { 
                            //创建连接是非常耗时的,所以不要每次向数据库zhong查数据时都创建连接!
                            using (SqlConnection conn = new SqlConnection(@"Data Source=EJNSWJOZ0JSDS7J;Initial Catalog=CSDNBoKe;Persist Security Info=True;User ID=sa;Password=111111"))
                            {
                                conn.Open();
                                using (SqlCommand cmd = conn.CreateCommand())
                                {
                                    cmd.CommandText = "insert T_user (Id,Fuser) values (@U,@A)";
                                    string line = null;
                                    while ((line = streamReader.ReadLine())!= null)
                                    {
                                        string[] strs = line.Split('|');
                                        string name = strs[1];
                                        int age = Convert.ToInt32(strs[0]);
                                        //!!!!!!参数不能重复添加!这里一直yong一个sqlcommand对象cmd
                                        cmd.Parameters.Clear();
                                        cmd.Parameters.Add(new SqlParameter("A",name));
                                        cmd.Parameters.Add(new SqlParameter("U", age));
                                        cmd.ExecuteNonQuery();
                                       
                                    }
                                }
                            }
                        }
                    }
                }
            }


            private void button2_Click(object sender, EventArgs e)
            {
               SaveFileDialog sfd = new SaveFileDialog();
                sfd.Filter = "文本文件|*.txt|网页文件|*.html|所有文件|*.*";
                if (sfd.ShowDialog() != DialogResult.OK)
                { return; }
                else
                {
                    
                   
                    using( FileStream fs = new FileStream(sfd.FileName, FileMode.Create))
                    {
                        using (StreamWriter sw = new StreamWriter(fs, Encoding.Default))
                        {
                            using (SqlConnection conn1 = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\inOrOut.mdf;Integrated Security=True;User Instance=True"))
                            {
                                conn1.Open();
                                using (SqlCommand cmd1 = conn1.CreateCommand())
                                {
                                    cmd1.CommandText = "select * from T_shuJu";
                                    using (SqlDataReader reader = cmd1.ExecuteReader())
                                    {
                                        while (reader.Read())
                                        {
                                            int ids = reader.GetInt32(reader.GetOrdinal("id"));
                                            string username = reader.GetString(reader.GetOrdinal("UserName"));
                                            int age = reader.GetInt32(reader.GetOrdinal("Age"));
                                            sw.Write(ids);
                                            sw.Write("\t"+username);
                                            sw.WriteLine(age);
                                        
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }




        }
    }

  • 相关阅读:
    430flash的操作
    430单片机之定时器A功能的大致介绍
    MSP430看门狗
    430之通用异步串口通信模块
    430的启动,I/O中断
    Msp430概述
    烦躁
    12864密码锁
    单片机的动手实践篇--51单片机玩转12864
    【CSS】font样式简写(转)- 不是很建议简写
  • 原文地址:https://www.cnblogs.com/qiqiBoKe/p/2791563.html
Copyright © 2011-2022 走看看