我正在使用 asp.net MVC 4 创建一个网站,用户可以在其中上传.xlsx
文件并将数据保存到 MSSQL 表中。我想在保存数据之前确保文件中没有非法字符,例如SQL注入语句。到目前为止,我用$
符号测试它工作正常,但它只会在一个单元格只有那个字符而不是在字符之间时才能捕捉到。这是我的代码,
控制器
public ActionResult BulkReadings()
{
string pathToExcelFile = System.IO.Path.Combine(Server.MapPath("~/ExcelFiles/"), "BulkReads.xlsx");
string sheetName = "Sheet1";
var excelFile = new ExcelQueryFactory(pathToExcelFile);
var getSheet = from a in excelFile.Worksheet(sheetName) select a;
string Subject = "";
string Type = "";
string Reading = "";
foreach (var a in getSheet)
{
if (a["Subject"] == "$" || a["Type"] == "$" || a["Reading"] == "$") // This is where it checks for the "$" sign
{
if (System.IO.File.Exists(pathToExcelFile))
{
System.IO.File.Delete(pathToExcelFile);
}
TempData["meter_fail"] = "Error! Illegal Characters!";
return RedirectToAction("MeterManager");
}
else
{
Subject = a["Subject"];
Type = a["Type"];
Reading = a["Reading"];
try
{
Reading newEntry = new Reading();
newEntry.title = Subject;
newEntry.type = Type;
newEntry.reading1 = Reading;
rentdb.Readings.Add(newEntry);
}
catch
{
if (System.IO.File.Exists(pathToExcelFile))
{
System.IO.File.Delete(pathToExcelFile);
}
TempData["meter_fail"] = "Error! Upload Failed!";
return RedirectToAction("MeterManager");
}
}
}
rentdb.SaveChanges();
if (System.IO.File.Exists(pathToExcelFile))
{
System.IO.File.Delete(pathToExcelFile);
}
TempData["meter_success"] = "Reading(s) uploaded successfully!";
return RedirectToAction("MeterManager");
}
如何检查多个非法字符,这些非法字符可以作为单个字符出现,也可以与单元格中的其他字符一起出现?