xaml重点代码:- <Grid>
- <TextBlock HorizontalAlignment="Left" Margin="10,21,0,0" TextWrapping="Wrap" Text="文本路径: " VerticalAlignment="Top"/>
- <TextBox Name="txtFileName" HorizontalAlignment="Left" Height="23" Margin="79,18,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="276"/>
- <Button Name="btnOpen" Content="浏览" HorizontalAlignment="Left" Margin="387,18,0,0" VerticalAlignment="Top" Width="75" Click="btnOpen_Click"/>
- <Button Name="btnImport" Content="文档汇入数据库" HorizontalAlignment="Left" Margin="200,122,0,0" VerticalAlignment="Top" Width="110" Click="btnImport_Click"/>
- <Button Name="btnExport" Content="数据库汇出文档" HorizontalAlignment="Left" Margin="352,122,0,0" VerticalAlignment="Top" Width="110" Click="btnExport_Click"/>
- </Grid>
复制代码 xaml.cs重点代码:- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Data;
- using System.Data.SqlClient;
- using System.IO;
- using System.Linq;
- using System.Windows;
- using Microsoft.Win32;
- namespace test1
- {
- /// <summary>
- /// case14.xaml 的交互逻辑
- /// </summary>
- public partial class case14 : Window
- {
- public case14()
- {
- InitializeComponent();
- }
- /// <summary>
- /// 浏览按钮方法
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnOpen_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- OpenFileDialog ofd = new OpenFileDialog();
- ofd.InitialDirectory = @"C:\Users\mavis\Desktop";
- ofd.Filter = @"TXT 文件|*.txt|所有文件|*.*";
- if (ofd.ShowDialog() == true)
- {
- txtFileName.Text = ofd.FileName;
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- /// <summary>
- /// 文档汇入数据库
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnImport_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- if (txtFileName.Text.Trim().Length == 0)
- {
- MessageBox.Show("请先选择要汇入的TXT文档!");
- return;
- }
- string connStr = ConfigurationManager.ConnectionStrings["dbConnStr"].ConnectionString;
- //File.ReadAllLines是把文件一次读取到string集合中
- //对于大的文件用流的方式 FileStream
- //乱码发生在保存和读取的编码不一致的情况下
- //File.ReadLines没有第二个参数的重载是采用UTF8编码
- IEnumerable<string> lines = File.ReadLines(txtFileName.Text.Trim());
- foreach (string line in lines)
- {
- string[] str = line.Split('\t');
- string name = str[0]; //姓名
- int age = Convert.ToInt32(str[1]); //年龄
- bool sex = Convert.ToBoolean(str[2]); //性别
- float height = float.Parse(str[3]); //身高
- float pay = float.Parse(str[4]); //工资
- using (SqlConnection conn = new SqlConnection(connStr))
- {
- conn.Open();
- using (SqlCommand cmd = conn.CreateCommand())
- {
- cmd.CommandText = "INSERT INTO dbo.T_Staff( Name, Age, Sex, Height, Pay )VALUES( @name, @age, @sex, @height, @pay)";
- cmd.Parameters.Add(new SqlParameter("@name", name));
- cmd.Parameters.Add(new SqlParameter("@age", age));
- cmd.Parameters.Add(new SqlParameter("@sex", sex));
- cmd.Parameters.Add(new SqlParameter("@height", height));
- cmd.Parameters.Add(new SqlParameter("@pay", pay));
- cmd.ExecuteNonQuery();
- }
- }
- }
- MessageBox.Show(lines.Count() + "条数据已经成功汇入数据库!");
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- /// <summary>
- /// 数据库汇出文档
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnExport_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- if (txtFileName.Text.Trim().Length == 0)
- {
- MessageBox.Show("请先选择要汇入的TXT文档!");
- return;
- }
- string connStr = ConfigurationManager.ConnectionStrings["dbConnStr"].ConnectionString;
- DataSet ds = new DataSet();
- using (SqlConnection conn = new SqlConnection(connStr))
- {
- conn.Open();
- using (SqlCommand cmd = conn.CreateCommand())
- {
- cmd.CommandText = "SELECT * FROM dbo.T_Staff";
- SqlDataAdapter adapter = new SqlDataAdapter(cmd);
- adapter.Fill(ds);
- }
- }
- DataRowCollection rows = ds.Tables[0].Rows;
- string[] str = new string[rows.Count];
- for (int i = 0; i < rows.Count; i++)
- {
- str[i] = rows[i]["Name"].ToString() + "\t" + rows[i]["Age"].ToString() + "\t" + rows[i]["Sex"].ToString() + "\t" + rows[i]["Height"].ToString() + "\t" + rows[i]["Pay"].ToString();
- }
- File.WriteAllLines(txtFileName.Text, str);
- MessageBox.Show("数据库资料汇出到TXT文档成功!");
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- }
- }
复制代码 |
|