2012-05-20 3 views
0

*.TXT 파일의 길이는 약 1 - 200 줄입니다. 각 줄은 City/Suburb의 이름 일 뿐이며이 파일의 각 줄을 데이터베이스에 삽입하려고합니다.줄 단위로 SQL Server CE 데이터베이스에 삽입합니다.

Try/Catch 블록은 작동하지 않았기 때문에 모두 넣었지만 예외가있을 경우 아무 것도 표시되지 않습니다.

db.Execute("insert into SuburbsPostCodesAndStates (Suburb, State) values (@0, @1)", "lol", "nsw"); 

내가이 라인을 삽입 사용하고 있음을 가지고있는 코드는 다음과 같습니다 :

@{ 
    var message = ""; 
    var db = Database.Open("SSSCCC"); 

    try 
    { 
     if(File.Exists(Href("~/NSW.txt"))) 
     { 
      string[] text = File.ReadAllLines(Href("~/NSW.txt")); 

      using (StringReader reader = new StringReader(text)) 
      { 
       string line; 

       while ((line = reader.ReadLine()) != null) 
       { 
        db.Execute("insert into SuburbsPostCodesAndStates (Suburb, State) values (@0, @1)", line, "NSW"); 
       } 
      } 
     } 
    } 
    catch(Exception exception) 
    { 
     message = exception.Message; 
    } 
} 

<!DOCTYPE html> 

<html lang="en"> 
    <head> 
     <meta charset="utf-8" /> 
     <title></title> 
    </head> 
    <body> 
     @message 
    </body> 
</html> 

난 그냥이 줄을 한 번에 한 레코드를 삽입 할 수 있음을 확인했다 이 문제를 어떻게 해결할 수 있습니까?

아무런 도움이 필요하지 않습니다.

당신에게

해결 감사합니다

@{ 
    var message = ""; 
    var db = Database.Open("SSSCCC"); 

    try 
    { 
     if(File.Exists(Server.MapPath("NSW.txt"))) 
     { 
      string[] text = File.ReadAllLines(Server.MapPath("NSW.txt")); 

      if(text.Length == 0) { throw new Exception("File does not contain any lines"); } 

      foreach(string line in text) 
      { 
       db.Execute("insert into SuburbsPostCodesAndStates (Suburb, State) values (@0, @1)", line, "NSW"); 

      } 
     } 
     else 
     { 
      throw new Exception("File does not exist"); 
     } 
    } 
    catch(Exception exception) 
    { 
     message = exception.Message; 
    } 
} 
+0

@ p.campbell - WebMatrix와 함께 제공되는 SQL Server Compact를 사용하고 있습니다. – Arrow

+0

어쩌면 별도의 삽입 문을 100 개 호출하는 대신 BULK INSERT를 고려해야합니다. –

+0

WebMatrix SQL Compact DB에 대량 삽입 할 수 없습니다 (WebMatrix를 사용하지 않기로 결정한 이유입니다). 호스팅 회사에서 제공 한 것이지만 아직 할 수는 없습니다. _ – Arrow

답변

1

당신이 여기에 StringReader를를 사용하는 이유를 잘 모르겠어요을 ... 대신을 시도합니다.

@{ 
      var message = ""; 
    var db = Database.Open("SSSCCC"); 
    try 
    { 
     if(File.Exists(Server.MapPath("~/NSW.txt"))) 
     { 
      string[] text = File.ReadAllLines(Server.MapPath("~/NSW.txt")); 
      if(text.Length == 0) throw new Exception("File does not contain any lines"); 
      foreach(string line in text) 
      { 
       db.Execute("insert into SuburbsPostCodesAndStates (Suburb, State) values (@0, @1)", line, "NSW"); 

      } 
     } 
     else 
     { 
      throw new Exception("File does not exist"); 
     } 
    } 
    catch(Exception exception) 
    { 
     message = exception.Message; 

    } 
} 
+0

같은 문제는 미안합니다. 이것은 작동하지 않았다. – Arrow

+0

몇 가지 예외를 추가하고 다른 예외를 던지면 예외가 발생하는지 확인합니다. –

+0

젠장! 나는 그 곳에 else 절을 ​​넣는 것을 알았다. 그것은 "파일이 존재하지 않습니다."라고 말합니다. 그것은 분명 거짓말입니다 ...하지만 적어도 나는 올바른 곳에서 찾고 있지 않다는 것을 알고 있습니다. Thanks for that @Chris Gessler – Arrow

관련 문제