2012-05-16 9 views
0

사용자 당 Excel 스프레드 시트를 업로드 한 다음 업로드 한 코드가 있으므로 SQL의 다른 SQL 테이블을 기반으로 SQL에서 열 중 하나를 업데이트합니다. Excel 스프레드 시트의 데이터가 SQL 테이블에서 업데이트 할 옵션 중 하나와 일치하는지 확인하고 싶습니다. 가장 쉬운 방법은 무엇입니까? 나는. NULL 값이 반환되거나 C#에서 복잡한 IF 문을 작성해야한다면 SQL 문을 추가하여 웹 사이트에 오류 메시지를 표시 할 수 있습니까? 여기에 내가 (SQL 문 참조) 지금까지이 작업은 다음과 같습니다C에서 작성하는 경우 오류 메시지를 보내려면 SQL 코드를 얻는 방법

try 
    { 

     FileUpload.SaveAs(path); 
     if (TextBox.Text != "") 
     sheetname = TextBox.Text; 
     System.Data.OleDb.OleDbConnection MyConnection; 
     System.Data.DataSet DtSet; 
     System.Data.OleDb.OleDbDataAdapter MyCommand; //check sheet name 
     MyConnection = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;"); 
     MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [" + sheetname + "$]", MyConnection); 
     //MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [July Noms$]", MyConnection); 
     MyCommand.TableMappings.Add("Table", "TestTable"); 
     DtSet = new System.Data.DataSet(); 
     MyCommand.Fill(DtSet); 
     SqlConnection curConnection = new SqlConnection(@"Data Source=1tc-Techcomm2;Initial Catalog=EventRegistration;Integrated Security=True"); 
     curConnection.Open(); 
     SqlCommand curCommand; 
     SqlParameter param; 
     string str; 

     for (int i = 0; i < DtSet.Tables[0].Rows.Count; i++) 
     { 
      if (DtSet.Tables[0].Rows[i][1] == DBNull.Value) 
       continue; 
      curCommand = curConnection.CreateCommand(); 
      curCommand.CommandText = @"Insert into TestSP (SessionHead_EventId, SessionHead_Title, SessionHead_TypeId, SessionHead_Description, SessionHead_Status, SessionHead_Presenter, SessionHead_Champion, SessionHead_AnnounceDate, SessionHead_Objectives, SessionHead_LaunchDate, SessionHead_Notes, SessionHead_Schedule, SessionHead_SpecialNeeds, SessionHead_Equipment, SessionHead_CDA) Values (@a,@b,@c,@d,@e,@f,@g,@h,@i,@j,@k,@l,@m,@n,@o)"; 
      for (int j = 0; j < 15; j++) 
      { 
       param = new SqlParameter(); 
       str = "@"; 
       str += (char)('a' + j); 
       param.ParameterName = str; 
       param.SqlDbType = SqlDbType.VarChar; 
       param.Value = DtSet.Tables[0].Rows[i][j]; 
       curCommand.Parameters.Add(param); 

      } 
      Label1.Text = "THE EXCEL DATE HAS SUCCESSFULLY BEEN SENT TO THE DATABASE"; 
      curCommand.ExecuteNonQuery(); 
      curCommand.CommandText = "UPDATE TestSP SET SessionHead_TypeId = (SELECT SessionType.SessionType_Id FROM SessionType WHERE SessionType.SessionType_Title = TestSP.SessionHead_TypeId) WHERE EXISTS (SELECT SessionType.SessionType_Id FROM SessionType WHERE SessionType.SessionType_Title = TestSP.SessionHead_TypeId)"; 
//****Can I add a statement here stating that if a NULL value is returned, to give me an error message on the website?**** 

     curCommand.ExecuteNonQuery(); 

     } 


     MyConnection.Close(); 
     curConnection.Close(); 

    } 
    catch (Exception ex) 
    { 
     //Response.Write(ex.ToString()); 
     Label1.Text = ex.Message; 
     Label2.Text = ex.Message; 

    } 


} 

어떤 도움은 매우 극명하게 될 것이다!

+1

예, 'ExecuteNonQuery'는 정수를 반환하므로 반환 값을 0으로 확인할 수 있습니다. 올바른 방법을 설명하지 않는 한. –

답변

1

저장 프로 시저를 사용하여 SQL 오류 메시지를 반환하는 것이 좋습니다.

0

캐치가 System.Data.SqlClient.SqlException이면 쿼리가 거짓 인 경우 오류 메시지가 표시됩니다. 또한 ExecuteNonQuery은 영향을받는 행에 정수를 제공합니다. 따라서 영향을받는 행이없는 경우 0이고 롤백과 같은 오류가있는 경우 -1입니다.

관련 문제