A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 彭家贰小姐 高级黑马   /  2013-8-10 23:28  /  1335 人查看  /  1 人回复  /   2 人收藏 转载请遵从CC协议 禁止商业使用本文



xaml重点代码:
  1. <Grid>
  2. <TextBlock HorizontalAlignment="Left" Margin="10,21,0,0" TextWrapping="Wrap" Text="文本路径: " VerticalAlignment="Top"/>
  3. <TextBox Name="txtFileName" HorizontalAlignment="Left" Height="23" Margin="79,18,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="276"/>
  4. <Button Name="btnOpen" Content="浏览" HorizontalAlignment="Left" Margin="387,18,0,0" VerticalAlignment="Top" Width="75" Click="btnOpen_Click"/>
  5. <Button Name="btnImport" Content="文档汇入数据库" HorizontalAlignment="Left" Margin="200,122,0,0" VerticalAlignment="Top" Width="110" Click="btnImport_Click"/>
  6. <Button Name="btnExport" Content="数据库汇出文档" HorizontalAlignment="Left" Margin="352,122,0,0" VerticalAlignment="Top" Width="110" Click="btnExport_Click"/>
  7. </Grid>
复制代码
xaml.cs重点代码:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Windows;
  9. using Microsoft.Win32;

  10. namespace test1
  11. {
  12. /// <summary>
  13. /// case14.xaml 的交互逻辑
  14. /// </summary>
  15. public partial class case14 : Window
  16. {
  17. public case14()
  18. {
  19. InitializeComponent();
  20. }

  21. /// <summary>
  22. /// 浏览按钮方法
  23. /// </summary>
  24. /// <param name="sender"></param>
  25. /// <param name="e"></param>
  26. private void btnOpen_Click(object sender, RoutedEventArgs e)
  27. {
  28. try
  29. {
  30. OpenFileDialog ofd = new OpenFileDialog();
  31. ofd.InitialDirectory = @"C:\Users\mavis\Desktop";
  32. ofd.Filter = @"TXT 文件|*.txt|所有文件|*.*";
  33. if (ofd.ShowDialog() == true)
  34. {
  35. txtFileName.Text = ofd.FileName;
  36. }
  37. }
  38. catch (Exception ex)
  39. {
  40. MessageBox.Show(ex.Message);
  41. }
  42. }

  43. /// <summary>
  44. /// 文档汇入数据库
  45. /// </summary>
  46. /// <param name="sender"></param>
  47. /// <param name="e"></param>
  48. private void btnImport_Click(object sender, RoutedEventArgs e)
  49. {
  50. try
  51. {
  52. if (txtFileName.Text.Trim().Length == 0)
  53. {
  54. MessageBox.Show("请先选择要汇入的TXT文档!");
  55. return;
  56. }
  57. string connStr = ConfigurationManager.ConnectionStrings["dbConnStr"].ConnectionString;
  58. //File.ReadAllLines是把文件一次读取到string集合中
  59. //对于大的文件用流的方式 FileStream
  60. //乱码发生在保存和读取的编码不一致的情况下
  61. //File.ReadLines没有第二个参数的重载是采用UTF8编码
  62. IEnumerable<string> lines = File.ReadLines(txtFileName.Text.Trim());
  63. foreach (string line in lines)
  64. {
  65. string[] str = line.Split('\t');
  66. string name = str[0]; //姓名
  67. int age = Convert.ToInt32(str[1]); //年龄
  68. bool sex = Convert.ToBoolean(str[2]); //性别
  69. float height = float.Parse(str[3]); //身高
  70. float pay = float.Parse(str[4]); //工资
  71. using (SqlConnection conn = new SqlConnection(connStr))
  72. {
  73. conn.Open();
  74. using (SqlCommand cmd = conn.CreateCommand())
  75. {
  76. cmd.CommandText = "INSERT INTO dbo.T_Staff( Name, Age, Sex, Height, Pay )VALUES( @name, @age, @sex, @height, @pay)";
  77. cmd.Parameters.Add(new SqlParameter("@name", name));
  78. cmd.Parameters.Add(new SqlParameter("@age", age));
  79. cmd.Parameters.Add(new SqlParameter("@sex", sex));
  80. cmd.Parameters.Add(new SqlParameter("@height", height));
  81. cmd.Parameters.Add(new SqlParameter("@pay", pay));
  82. cmd.ExecuteNonQuery();
  83. }
  84. }
  85. }
  86. MessageBox.Show(lines.Count() + "条数据已经成功汇入数据库!");
  87. }
  88. catch (Exception ex)
  89. {
  90. MessageBox.Show(ex.Message);
  91. }
  92. }

  93. /// <summary>
  94. /// 数据库汇出文档
  95. /// </summary>
  96. /// <param name="sender"></param>
  97. /// <param name="e"></param>
  98. private void btnExport_Click(object sender, RoutedEventArgs e)
  99. {
  100. try
  101. {
  102. if (txtFileName.Text.Trim().Length == 0)
  103. {
  104. MessageBox.Show("请先选择要汇入的TXT文档!");
  105. return;
  106. }
  107. string connStr = ConfigurationManager.ConnectionStrings["dbConnStr"].ConnectionString;
  108. DataSet ds = new DataSet();
  109. using (SqlConnection conn = new SqlConnection(connStr))
  110. {
  111. conn.Open();
  112. using (SqlCommand cmd = conn.CreateCommand())
  113. {
  114. cmd.CommandText = "SELECT * FROM dbo.T_Staff";
  115. SqlDataAdapter adapter = new SqlDataAdapter(cmd);
  116. adapter.Fill(ds);
  117. }
  118. }
  119. DataRowCollection rows = ds.Tables[0].Rows;
  120. string[] str = new string[rows.Count];
  121. for (int i = 0; i < rows.Count; i++)
  122. {
  123. 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();
  124. }
  125. File.WriteAllLines(txtFileName.Text, str);
  126. MessageBox.Show("数据库资料汇出到TXT文档成功!");
  127. }
  128. catch (Exception ex)
  129. {
  130. MessageBox.Show(ex.Message);
  131. }
  132. }
  133. }
  134. }
复制代码

1 个回复

倒序浏览
厉害哟{:soso_e179:}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马