数据导入与导出


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);
                                    
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }




    }
}

原文地址:https://www.cnblogs.com/qiqiBoKe/p/2791563.html