您好,登錄后才能下訂單哦!
C#中怎么讀取TXT文件,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
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) { } }
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。