C#中怎么读取TXT文件
C#中怎么读取TXT文件,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
创新互联公司公司2013年成立,先为息县等服务建站,息县等地企业,进行企业商务咨询服务。为息县企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
C#读取TXT文件之HTML的文件:
﹤body﹥ ﹤form id="form1" runat="server"﹥ ﹤div﹥ ﹤/div﹥ ﹤table border="0" cellpadding="0" cellspacing="0" style="width: 603px; height: 148px"﹥ ﹤tr﹥ ﹤td style="width: 100px"﹥ 序号:﹤asp:TextBox ID="TextBox1" runat="server"﹥ ﹤/asp:TextBox﹥﹤/td﹥ ﹤td style="width: 100px"﹥ 姓名:﹤asp:TextBox ID="TextBox2" runat="server"﹥ ﹤/asp:TextBox﹥﹤/td﹥ ﹤td style="width: 100px"﹥ 出生日期:﹤asp:TextBox ID="TextBox3" runat="server"﹥ ﹤/asp:TextBox﹥﹤/td﹥ ﹤/tr﹥ //C#读取TXT文件﹤tr﹥ ﹤td style="width: 100px"﹥ ﹤asp:FileUpload ID="FileUpload2" runat="server" /﹥ ﹤/td﹥ ﹤td style="width: 100px"﹥ ﹤asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="导入" /﹥ ﹤asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="查询" /﹥﹤/td﹥ ﹤td style="width: 100px"﹥ ﹤/td﹥ ﹤/tr﹥ ﹤/table﹥ ﹤/form﹥ ﹤/body﹥
C#读取TXT文件的C#代码:
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO; using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page { string id1, name, birthday, str,sfz,sex; SqlConnection con = new SqlConnection(); SqlDataReader dr = null; //int i,count= 1; protected void Page_Load(object sender, EventArgs e) { } //C#读取TXT文件 private void LeadInsert() { try { string path = FileUpload2.PostedFile.FileName; //定义上传路径 StreamReader smRead = new StreamReader(path, System.Text.Encoding.Default); //设置路径 string line; while ((line = smRead.ReadLine()) != null) { string[] arrStr = line.Split('|'); //分割符 “|” id1 = arrStr[0].ToString(); name = arrStr[1].ToString(); sfz = arrStr[2].ToString(); //birthday = arrStr[3].ToString(); //sex = arrStr[4].ToString(); if (Convert.ToInt32(sfz.Length.ToString())==18) { birthday = sfz.Substring(6, 8); DateTime mydt = DateTime.ParseExact(birthday, "yyyyMMdd", null); birthday = mydt.ToString("yyyy-MM-dd"); } if (Convert.ToInt32(sfz.Length.ToString())== 15) { birthday = "19"+sfz.Substring(6, 6).ToString(); DateTime mydt = DateTime.ParseExact(birthday, "yyyyMMdd", null); birthday = mydt.ToString("yyyy-MM-dd"); } if (Convert.ToInt32(sfz.Length.ToString()) == 18) { if (((Convert.ToInt16(sfz.Substring(16, 1).ToString())) % 2 == 0)) { sex = "0"; } else //C#读取TXT文件{ sex = "1"; } } if (Convert.ToInt32(sfz.Length.ToString()) == 15) { if ((( Convert.ToInt16(sfz.Substring(13, 1).ToString())) % 2 == 0)) { sex= "0"; } else { sex = "1"; } } string ConnStr = "server=localhost;uid=sa;pwd=sa;database=GAC"; con.ConnectionString = ConnStr; con.Open(); SqlCommand cmd = new SqlCommand(); cmd.CommandText = "insert into txt (name,birthday,sex,sfz) VALUES (@name,@birthday,@sex,@sfz)"; cmd.Connection = con; cmd.Parameters.Clear(); //cmd.Parameters.Add("@id1", SqlDbType.Int); //cmd.Parameters["@id1"].Value = Convert.ToInt32(id1); cmd.Parameters.Add("@name", SqlDbType.VarChar, 50); cmd.Parameters["@name"].Value = name.ToString(); cmd.Parameters.Add("@sfz", SqlDbType.VarChar, 20); cmd.Parameters["@sfz"].Value = sfz.ToString(); cmd.Parameters.Add("@birthday", SqlDbType.VarChar, 50); if (birthday.Length ﹥ 0) { cmd.Parameters["@birthday"].Value = birthday.ToString(); } else { cmd.Parameters["@birthday" ].Value = DBNull.Value; } cmd.Parameters.Add("@sex", SqlDbType.Char, 10); if (sex.Length ﹥ 0) { cmd.Parameters["@sex"].Value = Convert.ToInt16(sex.ToString()); } else { //C#读取TXT文件 cmd.Parameters["@sex"].Value = DBNull.Value; } cmd.ExecuteNonQuery(); con.Close(); } } catch (Exception ee) { } finally { } } protected void Button1_Click(object sender, EventArgs e) { LeadInsert(); } protected void Button2_Click(object sender, EventArgs e) { } } //C#读取TXT文件 using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO; using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page { string id1, name, birthday, str,sfz,sex; SqlConnection con = new SqlConnection(); SqlDataReader dr = null; //int i,count= 1; protected void Page_Load(object sender, EventArgs e) { } private void LeadInsert() { try { //C#读取TXT文件 string path = FileUpload2.PostedFile.FileName; StreamReader smRead = new StreamReader(path, System.Text.Encoding.Default); string line; while ((line = smRead.ReadLine()) != null) { string[] arrStr = line.Split('|'); id1 = arrStr[0].ToString(); name = arrStr[1].ToString(); sfz = arrStr[2].ToString(); //birthday = arrStr[3].ToString(); //sex = arrStr[4].ToString(); if (Convert.ToInt32(sfz.Length.ToString())==18) { birthday = sfz.Substring(6, 8); DateTime mydt = DateTime.ParseExact(birthday, "yyyyMMdd", null); birthday = mydt.ToString("yyyy-MM-dd"); } if (Convert.ToInt32(sfz.Length.ToString())== 15) { birthday = "19"+sfz.Substring(6, 6).ToString(); DateTime mydt = DateTime.ParseExact(birthday, "yyyyMMdd", null); birthday = mydt.ToString("yyyy-MM-dd"); } if (Convert.ToInt32(sfz.Length.ToString()) == 18) { if (((Convert.ToInt16(sfz.Substring(16, 1).ToString())) % 2 == 0)) { sex = "0"; } else { //C#读取TXT文件 sex = "1"; } } if (Convert.ToInt32(sfz.Length.ToString()) == 15) { if ((( Convert.ToInt16(sfz.Substring(13, 1).ToString())) % 2 == 0)) { sex= "0"; } else { sex = "1"; } } string ConnStr = "server=localhost;uid=sa;pwd=sa;database=GAC"; con.ConnectionString = ConnStr; con.Open(); SqlCommand cmd = new SqlCommand(); cmd.CommandText = "insert into txt (name,birthday,sex,sfz) VALUES (@name,@birthday,@sex,@sfz)"; cmd.Connection = con; cmd.Parameters.Clear(); //cmd.Parameters.Add("@id1", SqlDbType.Int); //cmd.Parameters["@id1"].Value = Convert.ToInt32(id1); cmd.Parameters.Add("@name", SqlDbType.VarChar, 50); cmd.Parameters["@name"].Value = name.ToString(); cmd.Parameters.Add("@sfz", SqlDbType.VarChar, 20); cmd.Parameters["@sfz"].Value = sfz.ToString(); cmd.Parameters.Add("@birthday", SqlDbType.VarChar, 50); if (birthday.Length ﹥ 0) { cmd.Parameters["@birthday"].Value = birthday.ToString(); } else { cmd.Parameters["@birthday" ].Value = DBNull.Value; } cmd.Parameters.Add("@sex", SqlDbType.Char, 10); if (sex.Length ﹥ 0) { cmd.Parameters["@sex"].Value = Convert.ToInt16(sex.ToString()); } else { //C#读取TXT文件 cmd.Parameters["@sex"].Value = DBNull.Value; } cmd.ExecuteNonQuery(); con.Close(); } } catch (Exception ee) { } finally { } } protected void Button1_Click(object sender, EventArgs e) { LeadInsert(); } protected void Button2_Click(object sender, EventArgs e) { } }
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。
文章标题:C#中怎么读取TXT文件
文章链接:http://scyingshan.cn/article/pcppdo.html