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>
<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#:
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
{
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
{
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
{
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)
{
}
}
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
{
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
{
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
{
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)
{
}
}