• C#将数据倒出到文本文件


    //html页面

    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <table>
                 <tr>
                     <th>用户编号</th>
                    <th>用户名</th>
                    <th>用户密码</th>
                    </tr>
            <asp:Repeater ID="rptData" runat="server">
                <ItemTemplate>
                <tr>
                   <td><%# Eval("u_id")%></td>
                   <td><%# Eval("u_Name") %></td>
                   <td><%# Eval("u_Pwd")%></td>
                   </tr>
                </ItemTemplate>
            </asp:Repeater>
        </table>
        </div>
        <p>
            <asp:Button ID="btnOutput" runat="server" Text="btnOutput"
                onclick="btnOutput_Click" />
        </p>
        </form>
    </body>
    </html>

    //后台代码

    using System;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Data.SqlClient;

    public partial class _Default : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection("server=.;database=Users;user id=sa;pwd=sa2000");

       private string sql = "select * from user_info";
        protected void Page_Load(object sender, EventArgs e)
        {
               
            SqlDataAdapter sda = new SqlDataAdapter(sql, con);
            DataSet ds = new DataSet();
            sda.Fill(ds);

            rptData.DataSource = ds;
            rptData.DataBind();

        }

        protected void Bind()
        {
            SqlDataAdapter sda = new SqlDataAdapter(sql, con);
            DataSet ds = new DataSet();
            sda.Fill(ds);

            DataTable dt = ds.Tables[0];

            if (dt.Rows.Count < 1)
            {
                Response.Write("<script>alert('没有数据!')</script>");
                return;
            }

            System.IO.StringWriter sw = new System.IO.StringWriter();
            sw.WriteLine("用户编号|用户名|用户密码");

            //遍历数据
            foreach (DataRow dr in dt.Rows)
            {
                sw.WriteLine(dr["u_id"].ToString().Trim() + "       |    " + dr["u_Name"].ToString().Trim() + "       |      " + dr["u_Pwd"].ToString().Trim());
            }
            sw.Close();
            Response.AddHeader("Content-Disposition", "attachment; filename=Test.txt");
            Response.ContentType = "text/txt";
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
            Response.Write(sw);
            Response.End();
        }

        protected void btnOutput_Click(object sender, EventArgs e)
        {
            Bind();
        }
    }

  • 相关阅读:
    Python服务Debian打包新思路
    小议Python3的原生协程机制
    推送公司今日菜单内容到手机
    Python包管理工具小结
    PAT 1068. 万绿丛中一点红
    PAT 1067. 试密码
    PAT 1066. 图像过滤
    PAT 1065. 单身狗
    PAT 1064. 朋友数
    PAT 1063. 计算谱半径
  • 原文地址:https://www.cnblogs.com/lgf8612/p/1707823.html
Copyright © 2020-2023  润新知