• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

c#读取TXT文本文件

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

HTML:
<body>
    <form >
    <div>
        &nbsp;</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)
    {

    }
}


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#验证输入的是否为数字类型的几种方法发布时间:2022-07-13
下一篇:
C#字符串的处理String和StringBuilder发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap