2011-03-16 4 views
0

여러 해 동안이 문제가 발생하지 않았으며 솔루션을 검색 할 때 찾을 수 없었습니다. SQL에서 오버로드라고 불리는 것 같습니다. 기본적으로이 SQL의 매개 변수에 대해 ""(빈 문자열)이 있는데 데이터베이스에 값을 설정하지 않으려합니다 ...이 일반적인 SQL 문제를 해결하는 방법

참고 : SQL 수준에서 수행하고 싶습니다. 그것의 부조리 한 방법 때문에 C# 수준에서.

string Sql = "IF NOT EXISTS (SELECT * FROM tbl_FileSystemReferences) " 
      + "INSERT INTO tbl_FileSystemReferences (UploadDir) VALUES (null) " 
      + "UPDATE tbl_FileSystemReferences SET " 
      + "[email protected], " 
      + "[email protected], " 
      + "[email protected], " 
      + "[email protected], " 
      + "[email protected], " 
      + "[email protected], " 
      + "[email protected], " 
      + "[email protected], " 
      + "[email protected], " 
      + "[email protected] "; 

SqlCommand Command = new SqlCommand(Sql); 
Command.Parameters.AddWithValue("@UploadDir", f.UploadDir); 
Command.Parameters.AddWithValue("@ThumbnailDir", f.ThumbnailDir); 
Command.Parameters.AddWithValue("@ArchiveDir", f.ArchiveDir); 
Command.Parameters.AddWithValue("@RealDir", f.RealDir); 
Command.Parameters.AddWithValue("@FlashDir", f.FlashDir); 
Command.Parameters.AddWithValue("@AssociatedFilesDir", f.AssociatedFilesDir); 
Command.Parameters.AddWithValue("@EnableArchiving", f.EnableArchiving); 
Command.Parameters.AddWithValue("@AppWideDir", f.AppWideDir); 
Command.Parameters.AddWithValue("@FFmpegDir", f.FFmpegDir); 
Command.Parameters.AddWithValue("@InstallationDir", f.InstallationDir); 

ExecuteNonQuery(Command); 

내가 거기에 알고 내가 그냥 캔트 .... (나는이 오버로드라고 생각한다)

건배,

답변

0

방법 중 하나가 될 것없는 빈 문자열 인 경우 명령에 추가 할 수 있습니다 그 방법 당신이 모든 매개 변수, 다음 쿼리 전에 중받을 저장 프로 시저 : 당신이

  • 당신이 만약 그들이 null로 각 매개 변수를 변환 null을 전달 할 수 있도록

    • 을 빈 등 :

      선택 @UploadDir = null의 경우 @UploadDir = ''

    당신이 다음 업데이트 쿼리에 모든 매개 변수에 대해 그렇게 할 것입니다 : SQL 그대로

    IF NOT EXISTS (SELECT * FROM tbl_FileSystemReferences) 
    INSERT INTO tbl_FileSystemReferences (UploadDir) VALUES (null) 
    UPDATE tbl_FileSystemReferences SET 
    UploadDir=coalesce(@UploadDir, UploadDir), 
    ThumbnailDir=coalesce(@ThumbnailDir, ThumbnailDir), 
    ArchiveDir=coalesce(@ArchiveDir, ArchiveDir), 
    RealDir=coalesce(@RealDir, RealDir), 
    FlashDir=coalesce(@FlashDir, FlashDir), 
    AssociatedFilesDir=coalesce(@AssociatedFilesDir, AssociatedFilesDir), 
    EnableArchiving=coalesce(@EnableArchiving, EnableArchiving), 
    AppWideDir=coalesce(@AppWideDir, AppWideDir), 
    FFmpegDir=coalesce(@FFmpegDir, FFmpegDir), 
    InstallationDir=coalesce(@InstallationDir, InstallationDir) 
    
  • 2

    당신이 만드는 방법을 기억 저장 프로 시저와 함께이 작업을 수행하는 데 사용되는 방법입니다 명령을 텍스트로 전달하는 대신 저장된 프로 시저?

    당신은 자신의 변수로 ", UploadDir = @ UploadDir"와 같은 각 줄을 끊고 오직이 null 또는

    +0

    가 정말 우리가 매개 변수와 함께 인라인 SQL을 사용 우리에게서 잔인한 것들이 너무 많아. 유용하다고해도 나는 당신의 말을 보았습니다. – Exitos