多年来没有遇到这个问题,当我搜索解决方案时,我找不到一个。我认为它在SQL中称为重载。基本上当我对这个SQL中的任何参数都有“”(空字符串)时,我不想在数据库中设置一个值...
注意:我想在SQL级别执行此操作,而不是在C#级别执行此操作,因为它的方式很草率。
string Sql = "IF NOT EXISTS (SELECT * FROM tbl_FileSystemReferences) "
+ "INSERT INTO tbl_FileSystemReferences (UploadDir) VALUES (null) "
+ "UPDATE tbl_FileSystemReferences SET "
+ "UploadDir=@UploadDir, "
+ "ThumbnailDir=@ThumbnailDir, "
+ "ArchiveDir=@ArchiveDir, "
+ "RealDir=@RealDir, "
+ "FlashDir=@FlashDir, "
+ "AssociatedFilesDir=@AssociatedFilesDir, "
+ "EnableArchiving=@EnableArchiving, "
+ "AppWideDir=@AppWideDir, "
+ "FFmpegDir=@FFmpegDir, "
+ "InstallationDir=@InstallationDir ";
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 :(得分:2)
您是否可以创建存储过程而不是将命令作为文本传递?
这样你可以将每个行(如“UploadDir = @ UploadDir”)分解为自己的变量,只有在它不为空或不为空的字符串时才将其添加到命令
答案 1 :(得分:0)
一种方法是在存储过程中,您将接收所有这些参数,然后在查询之前:
如果它们为空,则将每个参数转换为null:
选择@UploadDir = null其中@UploadDir =''
你会为所有参数做到这一点,然后是更新查询:
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)