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
{
publicForm1()
{
InitializeComponent();
}
privatevoid button1_Click(objectsender,EventArgs e)
{
//当点击文件对话框的确定按钮时打开相应的文件
if(openFileDialog1.ShowDialog()==DialogResult.OK)
{
//使用FileStream类从文本中读数据,文件名为“openFileDialog1.FileName”
using(FileStream filestream =File.OpenRead(openFileDialog1.FileName))
{
//读取文件中的信息。Encoding.Default是操作系统默认的编码页,如不加这句导入到表中的数据可能出现乱码
using(StreamReader streamreader =newStreamReader(filestream,Encoding.Default))
{
string line =null;
while ((line = streamreader.ReadLine()) !=null)
{
string[] str = line.Split(newchar[]{'|'},StringSplitOptions.RemoveEmptyEntries);
string name = str[0];
string age = str[1];
using (SqlConnection conn =newSqlConnection("datasource=凡斌-VAIO;Initial catalog=sales; integrated security=true"))
{
conn.Open();
using (SqlCommandcmd = conn.CreateCommand())
{
cmd.CommandText = "insert into t_user(name,age)values(@name,@age)";
cmd.Parameters.Add(newSqlParameter("name",name));
cmd.Parameters.Add(newSqlParameter("age", age));
cmd.ExecuteNonQuery();
}
}
}
}
}
MessageBox.Show("导入成功");
}
}
}
}
StreamReader 旨在以一种特定的编码输入字符,而 Stream类用于字节的输入和输出。使用 StreamReader读取标准文本文件的各行信息。除非另外指定,StreamReader的默认编码为 UTF-8,而不是当前系统的 ANSI代码页。
UTF-8 可以正确处理 Unicode字符并在操作系统的本地化版本上提供一致的结果。
默认情况下,StreamReader不是线程安全的。有关线程安全包装的信息
WPF流文件的处理
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.IO;
namespace WpfApplication1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
//表示通过对话框,用户可以使用此对话框来指定一个或多个要打开的文件的文件名
OpenFileDialog ofd = new OpenFileDialog();
//如果选中了文件,并点击了确定(打开)
if (ofd.ShowDialog() == true)
{
//打开现有文件进行读取。ofd.FileName的意思是“在文件对话框中选定的文件的完整路径”
using (FileStream filestream = File.OpenRead(ofd.FileName))
{
//创建一个StreamReader类的实例
using (StreamReader streamreader = new StreamReader(filestream, Encoding.Default))
{
//从流的当前位置到末尾读取流,并将数据作为字符串返回
string line = streamreader.ReadToEnd();
if (line != null)
{
MessageBox.Show(line);
}
else
{
MessageBox.Show("空文件");
}
}
}
}
else
{
MessageBox.Show("为什么不选择打开,而选择取消呢?");
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.IO;
namespace 数据流处理
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnBigIData_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
//弹出一个对话框,让用户选要读取的文件,如果用户选中了文件
if (ofd.ShowDialog() == true)
{
//当我选中D盘下的“C#教程的时候”其实这个fileName等于D:\C#教程.txt
//string fileName = ofd.FileName;
//读取选中文件的内容。因为ReadAllLines是一行一行的读取,所以它的返回值是一个数组
//string[] str1 = File.ReadAllLines(ofd.FileName, Encoding.Default).ToArray();
//现在来读取刚刚选中的文件,赋给一个FileStream类型的字节流变量
using(FileStream stream=File.OpenRead(ofd.FileName))
{
//刚刚读取了文件了,那么现在实现一个 System.IO.TextReader,使其以一种特定的编码从字节流中读取字符。
//用指定的字符编码为指定的流初始化 System.IO.StreamReader 类的一个新实例
using (StreamReader reader = new StreamReader(stream, Encoding.Default))
{
//从当前流中读取一行字符并将数据作为字符串返回。输入流中的下一行;如果到达了输入流的末尾,则为 null。
string ling = reader.ReadToEnd();
//如果一直有数据,那么我就对数据进行处理了
if (ling != null)
{
string[] str = ling.Split(' ');
string Name = str[0];
string Age = str[1];
}
}
}
}
}
}
}