2010-04-15 88 views
0

如何使用asp.net(C#)如何使用asp.net(C#)

+2

你真的需要存儲在數據庫中的文件,或僅僅是內容?如果只是內容,然後只讀取它並將文本存儲在Varchar(MAX)列中。 – 2010-04-15 10:31:15

+0

你能更具體嗎? txt文件與邊的SQL,女巫必須執行,或txt文件與一些隨機文本里面只存儲信息? – lfx 2010-04-15 10:31:40

回答

0

上傳.txt文件到SQL數據庫,這個東西會由用戶手動完成上傳.txt文件到SQL數據庫?

如果是這樣,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(); 
    } 
}