C#读取TXT文件之HTML的文件:

  1. ﹤body﹥  
  2.     ﹤form id="form1" runat="server"﹥  
  3.     ﹤div﹥  
  4.          ﹤/div﹥  
  5.         ﹤table border="0" cellpadding="0"   
  6. cellspacing="0" style="width: 603px; height: 148px"﹥  
  7. ﹤tr﹥  
  8.     ﹤td style="width: 100px"﹥  
  9.         序号:﹤asp:TextBox ID="TextBox1" runat="server"﹥  
  10. ﹤/asp:TextBox﹥﹤/td﹥  
  11.     ﹤td style="width: 100px"﹥  
  12.         姓名:﹤asp:TextBox ID="TextBox2" runat="server"﹥  
  13. ﹤/asp:TextBox﹥﹤/td﹥  
  14.     ﹤td style="width: 100px"﹥  
  15.         出生日期:﹤asp:TextBox ID="TextBox3" runat="server"﹥  
  16. ﹤/asp:TextBox﹥﹤/td﹥  
  17. ﹤/tr﹥  //C#读取TXT文件
  18. ﹤tr﹥  
  19.     ﹤td style="width: 100px"﹥  
  20.         ﹤asp:FileUpload ID="FileUpload2" runat="server" /﹥  
  21. ﹤/td﹥  
  22.     ﹤td style="width: 100px"﹥  
  23.         ﹤asp:Button ID="Button1" runat="server"   
  24. OnClick="Button1_Click" Text="导入" /﹥  
  25.         ﹤asp:Button ID="Button2" runat="server"   
  26. OnClick="Button2_Click" Text="查询" /﹥﹤/td﹥  
  27.     ﹤td style="width: 100px"﹥  
  28.     ﹤/td﹥  
  29. ﹤/tr﹥  
  30.         ﹤/table﹥  
  31.     ﹤/form﹥  
  32. ﹤/body﹥ 

C#读取TXT文件的C#代码:

  1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Collections;  
  5. using System.Web;  
  6. using System.Web.Security;  
  7. using System.Web.UI;  
  8. using System.Web.UI.WebControls;  
  9. using System.Web.UI.WebControls.WebParts;  
  10. using System.Web.UI.HtmlControls;  
  11. using System.IO;  
  12. using System.Data.SqlClient;  
  13.  
  14. public partial class _Default : System.Web.UI.Page  
  15. {  
  16.     string id1, name, birthday, str,sfz,sex;   
  17.     SqlConnection con = new SqlConnection();  
  18.     SqlDataReader dr = null;  
  19.     //int i,count= 1;  
  20.     protected void Page_Load(object sender, EventArgs e)  
  21.     {  
  22.  
  23.     }  //C#读取TXT文件
  24.     private void LeadInsert()  
  25.     {  
  26.         try 
  27.         {  
  28.  
  29. string path = FileUpload2.PostedFile.FileName; //定义上传路径  
  30. StreamReader smRead = new StreamReader(path,   
  31. System.Text.Encoding.Default); //设置路径  
  32. string line;  
  33. while ((line = smRead.ReadLine()) != null)   
  34. {  
  35.     string[] arrStr = line.Split('|');     //分割符 “|”  
  36.     id1 = arrStr[0].ToString();  
  37.     name = arrStr[1].ToString();  
  38.     sfz = arrStr[2].ToString();  
  39.     //birthday = arrStr[3].ToString();  
  40.     //sex = arrStr[4].ToString();  
  41.     if (Convert.ToInt32(sfz.Length.ToString())==18)  
  42.     {  
  43.         birthday = sfz.Substring(6, 8);  
  44.         DateTime mydt = DateTime.ParseExact(birthday, "yyyyMMdd", null);  
  45.         birthday = mydt.ToString("yyyy-MM-dd");  
  46.     }  
  47.     if (Convert.ToInt32(sfz.Length.ToString())== 15)  
  48.     {  
  49.         birthday = "19"+sfz.Substring(6, 6).ToString();  
  50.         DateTime mydt = DateTime.ParseExact(birthday, "yyyyMMdd", null);  
  51.         birthday = mydt.ToString("yyyy-MM-dd");  
  52.     }  
  53.     if (Convert.ToInt32(sfz.Length.ToString()) == 18)  
  54.     {  
  55.         if (((Convert.ToInt16(sfz.Substring(16, 1).ToString())) % 2 == 0))  
  56.            {  
  57.  sex = "0";  
  58. }  
  59. else  //C#读取TXT文件
  60. {  
  61.    sex = "1";  
  62. }  
  63.         }  
  64.         if (Convert.ToInt32(sfz.Length.ToString()) == 15)  
  65.         {  
  66. if ((( Convert.ToInt16(sfz.Substring(13, 1).ToString())) % 2 == 0))  
  67. {  
  68.     sex= "0";  
  69. }  
  70. else 
  71. {  
  72.    sex = "1";  
  73. }  
  74.         }  
  75.     string ConnStr = "server=localhost;uid=sa;pwd=sa;database=GAC";  
  76.     con.ConnectionString = ConnStr;  
  77.     con.Open();  
  78.     SqlCommand cmd = new SqlCommand();  
  79.     cmd.CommandText = "insert into txt (name,birthday,sex,sfz)  
  80.  VALUES (@name,@birthday,@sex,@sfz)";  
  81.     cmd.Connection = con;  
  82.     cmd.Parameters.Clear();  
  83.     //cmd.Parameters.Add("@id1", SqlDbType.Int);  
  84.     //cmd.Parameters["@id1"].Value = Convert.ToInt32(id1);  
  85.     cmd.Parameters.Add("@name", SqlDbType.VarChar, 50);  
  86.     cmd.Parameters["@name"].Value = name.ToString();  
  87.     cmd.Parameters.Add("@sfz", SqlDbType.VarChar, 20);  
  88.     cmd.Parameters["@sfz"].Value = sfz.ToString();  
  89.     cmd.Parameters.Add("@birthday", SqlDbType.VarChar, 50);  
  90.     if (birthday.Length ﹥ 0)  
  91.     {  
  92.         cmd.Parameters["@birthday"].Value = birthday.ToString();  
  93.     }  
  94.     else {  
  95.         cmd.Parameters["@birthday" ].Value = DBNull.Value;  
  96.      }  
  97.     cmd.Parameters.Add("@sex", SqlDbType.Char, 10);  
  98.     if (sex.Length ﹥ 0)  
  99.     {  
  100.         cmd.Parameters["@sex"].Value = Convert.ToInt16(sex.ToString());  
  101.     }  
  102.     else 
  103.     {  //C#读取TXT文件
  104.         cmd.Parameters["@sex"].Value = DBNull.Value;  
  105.     }  
  106.     cmd.ExecuteNonQuery();  
  107.     con.Close();  
  108.           }  
  109.         }  
  110.         catch (Exception ee) { }  
  111.         finally {  
  112.              
  113.         }  
  114.  
  115.     }  
  116.     protected void Button1_Click(object sender, EventArgs e)  
  117.     {  
  118.         LeadInsert();  
  119.     }  
  120.  
  121.     protected void Button2_Click(object sender, EventArgs e)  
  122.     {  
  123.  
  124.     }  
  125. }  
  126.  //C#读取TXT文件
  127.  
  128. using System;  
  129. using System.Data;  
  130. using System.Configuration;  
  131. using System.Collections;  
  132. using System.Web;  
  133. using System.Web.Security;  
  134. using System.Web.UI;  
  135. using System.Web.UI.WebControls;  
  136. using System.Web.UI.WebControls.WebParts;  
  137. using System.Web.UI.HtmlControls;  
  138. using System.IO;  
  139. using System.Data.SqlClient;  
  140.  
  141. public partial class _Default : System.Web.UI.Page  
  142. {  
  143.     string id1, name, birthday, str,sfz,sex;  
  144.     SqlConnection con = new SqlConnection();  
  145.     SqlDataReader dr = null;  
  146.     //int i,count= 1;  
  147.     protected void Page_Load(object sender, EventArgs e)  
  148.     {  
  149.  
  150.     }  
  151.     private void LeadInsert()  
  152.     {  
  153.         try 
  154.         {  //C#读取TXT文件
  155.  
  156. string path = FileUpload2.PostedFile.FileName;  
  157. StreamReader smRead = new StreamReader(path,  
  158.  System.Text.Encoding.Default);  
  159. string line;  
  160. while ((line = smRead.ReadLine()) != null)  
  161. {  
  162.     string[] arrStr = line.Split('|');  
  163.     id1 = arrStr[0].ToString();  
  164.     name = arrStr[1].ToString();  
  165.     sfz = arrStr[2].ToString();  
  166.     //birthday = arrStr[3].ToString();  
  167.     //sex = arrStr[4].ToString();  
  168.     if (Convert.ToInt32(sfz.Length.ToString())==18)  
  169.     {  
  170.         birthday = sfz.Substring(6, 8);  
  171.         DateTime mydt = DateTime.ParseExact(birthday, "yyyyMMdd", null);  
  172.         birthday = mydt.ToString("yyyy-MM-dd");  
  173.     }  
  174.     if (Convert.ToInt32(sfz.Length.ToString())== 15)  
  175.     {  
  176.         birthday = "19"+sfz.Substring(6, 6).ToString();  
  177.         DateTime mydt = DateTime.ParseExact(birthday, "yyyyMMdd", null);  
  178.         birthday = mydt.ToString("yyyy-MM-dd");  
  179.     }  
  180.     if (Convert.ToInt32(sfz.Length.ToString()) == 18)  
  181.     {  
  182.         if (((Convert.ToInt16(sfz.Substring(16, 1).ToString())) % 2 == 0))  
  183.            {  
  184.  sex = "0";  
  185. }  
  186. else 
  187. {  //C#读取TXT文件
  188.    sex = "1";  
  189. }  
  190.         }  
  191.         if (Convert.ToInt32(sfz.Length.ToString()) == 15)  
  192.         {  
  193. if ((( Convert.ToInt16(sfz.Substring(13, 1).ToString())) % 2 == 0))  
  194. {  
  195.     sex= "0";  
  196. }  
  197. else 
  198. {  
  199.    sex = "1";  
  200. }  
  201.         }  
  202.     string ConnStr = "server=localhost;uid=sa;pwd=sa;database=GAC";  
  203.     con.ConnectionString = ConnStr;  
  204.     con.Open();  
  205.     SqlCommand cmd = new SqlCommand();  
  206.     cmd.CommandText = "insert into txt (name,birthday,sex,sfz) VALUES (@name,@birthday,@sex,@sfz)";  
  207.     cmd.Connection = con;  
  208.     cmd.Parameters.Clear();  
  209.     //cmd.Parameters.Add("@id1", SqlDbType.Int);  
  210.     //cmd.Parameters["@id1"].Value = Convert.ToInt32(id1);  
  211.     cmd.Parameters.Add("@name", SqlDbType.VarChar, 50);  
  212.     cmd.Parameters["@name"].Value = name.ToString();  
  213.     cmd.Parameters.Add("@sfz", SqlDbType.VarChar, 20);  
  214.     cmd.Parameters["@sfz"].Value = sfz.ToString();  
  215.     cmd.Parameters.Add("@birthday", SqlDbType.VarChar, 50);  
  216.     if (birthday.Length ﹥ 0)  
  217.     {  
  218.         cmd.Parameters["@birthday"].Value = birthday.ToString();  
  219.     }  
  220.     else {  
  221.         cmd.Parameters["@birthday" ].Value = DBNull.Value;  
  222.      }  
  223.     cmd.Parameters.Add("@sex", SqlDbType.Char, 10);  
  224.     if (sex.Length ﹥ 0)  
  225.     {  
  226.         cmd.Parameters["@sex"].Value = Convert.ToInt16(sex.ToString());  
  227.     }  
  228.     else 
  229.     {  //C#读取TXT文件
  230.         cmd.Parameters["@sex"].Value = DBNull.Value;  
  231.     }  
  232.     cmd.ExecuteNonQuery();  
  233.     con.Close();  
  234.           }  
  235.         }  
  236.         catch (Exception ee) { }  
  237.         finally {  
  238.              
  239.         }  
  240.  
  241.     }  
  242.     protected void Button1_Click(object sender, EventArgs e)  
  243.     {  
  244.         LeadInsert();  
  245.     }  
  246.  
  247.     protected void Button2_Click(object sender, EventArgs e)  
  248.     {  
  249.  
  250.     }  
  251. }




http://developer.51cto.com/art/200908/143651.htm
posted on 2010-01-12 16:34  superlee  阅读(1589)  评论(0编辑  收藏  举报