简体   繁体   中英

how to upload .txt files to sql database using asp.net(C#)

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

Is this something that will be done manually by a user?

If so, how about an ASP.NET MVC application?

In your 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();
%>

In your 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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM