如何使用asp.net将.txt文件上传到sql数据库(C#)

时间:2010-04-15 10:28:39

标签: c# asp.net sql text-files

如何使用asp.net(C#)将.txt文件上传到sql数据库

1 个答案:

答案 0 :(得分:0)

这是否会由用户手动完成?

如果是这样,ASP.NET MVC申请怎么样?

View

<h2>
    Upload A File of Foos</h2>
<%
    Html.BeginForm("LoadFoos", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" });
%>
<label for="foosFile">
    Foos file:
</label>
<input type="file" name="FileUpload1" id="foosFile" /><br />
<input type="submit" name="Submit" id="Submit" value="Upload" />
<%
    Html.EndForm();
%>

Controller

public class AdminController : Controller
{
    public ActionResult LoadFoos()
    {
        if (Request.Files.Count > 0)
        {
            // This illustrates how to read the uploaded file contents,
            // and would need to be adapted to your scenario
            List<string> foos = new List<string>();

            using (StreamReader reader = new StreamReader(Request.Files[0].InputStream))
            {
                while (!reader.EndOfStream)
                {
                    string line = reader.ReadLine();

                    foos.Add(line);
                }
            }

            // TODO: use LINQ 2 SQL, NHibernate or ADO.NET to load the foos directly,
            // or call a web/WCF service behind your firewall that does this
        }

        return View();
    }
}