文件的导入导出

 
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.IO;
using System.Data.SqlClient;

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

        private void btnImport_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)//左边bool值,右边枚举型,怎么能够相等?
            {
                //FileStream fileStream = File.OpenRead(openFileDialog1.FileName);
                using (FileStream fileStream = new FileStream(openFileDialog1.FileName, FileMode.OpenOrCreate, FileAccess.Read))//这两条语句
                {
                    using (StreamReader streamReader = new StreamReader(fileStream))
                    {
                        string line = null;
                        while ((line = streamReader.ReadLine()) != null)
                        {
                            string[] str = line.Split('|');
                            string name = str[0];
                            int age = int.Parse(str[1]);
                            string strConn = @"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\传智博客练习\AdoNet\文件导入导出\Person.mdf;Integrated Security=True;User Instance=True";
                            using (SqlConnection conn = new SqlConnection(strConn))
                            {
                                conn.Open();
                                using (SqlCommand cmd = conn.CreateCommand())
                                {
                                    cmd.CommandText = "insert into T_Person(Name,Age) values(@NM,@AG)";
                                    cmd.Parameters.Add(new SqlParameter("NM", name));
                                    cmd.Parameters.Add(new SqlParameter("AG", age));
                                    cmd.ExecuteNonQuery();
                                }
                            }
                        }
                        MessageBox.Show("插入成功"); // 删除数据表全部内容 delete from T_Person;
                    }
                }
            }
        }
        private void btnExport_Click(object sender, EventArgs e)
        {

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                #region
                string strConn = @"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\传智博客练习\AdoNet\文件导入导出\Person.mdf;Integrated Security=True;User Instance=True";
                using (SqlConnection conn = new SqlConnection(strConn))
                {
                    conn.Open();
                    using (SqlCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = "Select * from T_Person";
                        using (SqlDataReader dataReader = cmd.ExecuteReader())
                        {                            
                            int count = 0;
                            while (dataReader.Read())
                            {                                
                                int ID = dataReader.GetInt32(dataReader.GetOrdinal("id"));
                                string NAME = dataReader.GetString(dataReader.GetOrdinal("Name"));
                                int AGE = dataReader.GetInt32(dataReader.GetOrdinal("Age"));//dataReader必须用.GetInt32,不可以parse什么的。
                                File.AppendAllText(Path.GetFullPath(saveFileDialog1.FileName), ID + "|"+NAME + "|"+AGE+"\r\n"); // 文件中换行"\r\n"
                                count++;                                
                            }
                            MessageBox.Show(count + "条记录被导出");
                        }
                    }
                }
                #endregion
            }
            else
            {
                return;
            }
        }
    }
}

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值