2011-12-08 5 views
1

VS2005 C# ASP.NET과 SQL Server 2005를 사용하고 있습니다.가져 오기 전에 파일 내용을 확인하십시오.

Excel 데이터를 가져 오는 기능이 있습니다. 내부의 데이터가 부적절한 상황을 만났을 때 SQL Server DB가 다운됩니다.

예. SELECT [Username], [Userlist $]에서 [Password] -> Excel 스프레드 시트에 [Username]이 (가) 한 열 또는 열 아래에 있으면 서버가 중단됩니다.

E.G. enter image description here

업로드하기 전에이 파일의 오류를 확인하는 방법을 어떻게 알 수 있습니까? 확인을 위해 ifelse 진술을 선호하십시오.

주어진 도움이나 예제를 이용해 주셔서 감사합니다. 다음은

는 엑셀 업로드 내 코드입니다 :

if (FileImport.HasFile) 
    { 

     // Get the name of the Excel spreadsheet to upload. 
     string strFileName = Server.HtmlEncode(FileImport.FileName); 

     // Get the extension of the Excel spreadsheet. 
     string strExtension = Path.GetExtension(strFileName); 

     // Validate the file extension. 
     if (strExtension == ".xls" || strExtension == ".xlsx") 
     { 
       // Generate the file name to save. 
       string strUploadFileName = "C:/Documents and Settings/user01/My Documents/Visual Studio 2005/WebSites/MajorProject/UploadFiles/" + DateTime.Now.ToString("yyyyMMddHHmmss") + strExtension; 

       // Save the Excel spreadsheet on server. 
       FileImport.SaveAs(strUploadFileName); 

       // Create Connection to Excel Workbook 
       string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strUploadFileName + ";Extended Properties=Excel 8.0;"; 

       using (OleDbConnection connection = 
          new OleDbConnection(connStr)) 
       { 
        string selectStmt = string.Format("Select [COLUMNS] FROM [userlist$]"); 

        OleDbCommand command = new OleDbCommand(selectStmt, connection); 

        connection.Open(); 
        Console.WriteLine("Connection Opened"); 
        // Create DbDataReader to Data Worksheet 
        using (DbDataReader dr = command.ExecuteReader()) 
        { 
         // SQL Server Connection String 
         string sqlConnectionString = "Data Source=<datasource>"; 

         // Bulk Copy to SQL Server 
         using (SqlBulkCopy bulkCopy = 
            new SqlBulkCopy(sqlConnectionString)) 
         { 
          bulkCopy.DestinationTableName = "UserDB"; 
          bulkCopy.WriteToServer(dr); 
          return; 
         } 
        } 
       } 
+0

전체 데이터베이스 서버가 다운되는 것을 말하고 있습니까? –

+0

@ LasseV.Karlsen 네,'원격 호스트가 원격 호스트에 의해 강제로 닫히고, 서버를 재시작해야 다시 연결될 수 있습니다. – user1084683

답변

-1

데이터베이스가 손상 될 이유는 없습니다. 가변 길이가 올바르게 선언되었는지 확인하십시오.

+0

이것은 유효한 대답이 아닙니다. 어떤 가변 길이가 올바르게 선언되어야 하는가? 이것은 원래 질문과는 아무런 관련이 없습니다. –

0

Excel 스프레드 시트에있는 데이터의 양에 따라, 당신은 당신이에 관심이있는 열에서 각 값을 읽을 수 있습니다 사전 예를 들어, 첫 번째 충돌을 발견하자마자 가져 오기를 실패합니다. 기존 datareeader를 사용하여 예를 들어

는 :

Dictionary<string, string> cValues = new Dictionary<string, string>(); 

// Create DbDataReader to Data Worksheet 
using (DbDataReader dr = command.ExecuteReader()) 
{ 
    while (dr.Read()) { 
     string sValue = dr[0].ToString(); 
     if (cValue.ContainsKey(sValue)) { 
     // There is a duplicate value, so bail 
     throw new Exception("Duplicate value " + sValue); 
     } else { 
     cValues.Add(sValue, sValue); 
     } 
    } 
} 

// Now execute the reader on the command again to perform the upload 
using (DbDataReader dr = command.ExecuteReader()) 
0

당신은 데이터베이스입니다 문제의 원인을 해결해야한다. 분명히, 데이터를 저장하는 방법은 잘못 입력 된 데이터를 처리하는 저장 프로 시저를 통해 이루어져야합니다.

또한 데이터베이스 테이블의 제약으로 인해 비 감각적 인 데이터를 저장하지 않아야합니다.

관련 문제