2016-08-15 19 views
0

上傳時解析一個文本文件,我有一個web項目,我需要:在.NET

1.Browse一個文件並上傳 2.解析文件在分號 3.提交文件到服務器

正在上傳的文件是每個SQL語句以分號(;)結尾的.sql文件。這個想法將是每行解析1個SQL語句。 (源文件可能有跨越20行的SQL語句,但第20行的分號表示語句分成一行。

這對大多數人來說都是新手,但到目前爲止,按鈕,搜索文件,並將其保存到本地驅動器,稍後我會擔心確切的存儲位置,但我現在對如何解析.sql文件感興趣,如上所述。進入我的代碼

感謝

HTML:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="FileUpload.WebForm1" %> 

<!DOCTYPE html> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <%-- <form id="form1" runat="server"> --%> 
     <form id="form2" runat="server"> 
    <asp:FileUpload id="FileUploadControl" runat="server" /> 
    <asp:Button runat="server" id="UploadButton" text="Upload" onclick="UploadButton_Click" /> 
    <br /><br /> 
    <asp:Label runat="server" id="StatusLabel" text="Upload status: " /> 


</form> 
</body> 
</html> 

CODE:

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

namespace FileUpload 
{ 
    public partial class WebForm1 : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 

     } 

     protected void UploadButton_Click(object sender, EventArgs e) 
     { 
      try 
      { 
       string filename = Path.GetFileName(FileUploadControl.FileName); 
       FileUploadControl.SaveAs(Server.MapPath("~/") + filename); 
       StatusLabel.Text = "Upload status: File uploaded!"; 
      } 
      catch (Exception ex) 
      { 
       StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message; 
      } 
     } 
    } 
} 
+0

閱讀拆分在分號(string.Split),字符串陣列上工作返回的返回字符串中的每個元素是該文件(File.ReadAllText)你的'單排' – Steve

回答