我试图将我的 sql 语句存储在资源文件夹中的 .sql 文件中,
我需要将 2 个值传递给 sql 命令,但我很难做到这一点
我的sql如下:
SELECT * FROM claim_header WHERE @columnname = @testcase;
我的班级称它为:
class DataValidation
{
public string testCase { get; set; }
public string elementName { get; set; }
public string fileDestination = ConfigurationSettings.AppSettings["ValidationReportDestination"];
public List<string> testCasesList = new List<string>();
public void PrintValidationResults()
{
int counter = 0;
SqlConnection sqlConnection = new SqlConnection(constring);
string sqlConnectionStringPassed = "select * from claim_header";
string sqlConnectionStringFailed = "select * from dbo.[SSIS Exception Info] where data_entity_name like '%CLAIM%' and package_name like '%Extract%' order by 1 desc;";
SqlDataReader myReaderPassed, myReaderFailed = null;
SqlCommand sqlCommandPassed = new SqlCommand(sqlConnectionStringPassed, sqlConnection);
SqlCommand sqlCommandFailed = new SqlCommand(sqlConnectionStringFailed, sqlConnection);
string sqlPass = EmbeddedResource.GetString("PassQuery.sql");
SqlCommand cmd = new SqlCommand(sqlPass,sqlConnection);
cmd.Parameters.AddWithValue("@testcase", "'" + testCasesList[counter] + "'");
cmd.Parameters.AddWithValue("@columnname", elementName);
try
{
sqlConnection.Open();
myReaderPassed = sqlCommandPassed.ExecuteReader();
myReaderFailed = sqlCommandFailed.ExecuteReader();
object query = cmd.ExecuteScalar();
string test = cmd.CommandText;
//Print Passed Results
Directory.CreateDirectory(fileDestination);
using (StreamWriter resultWriter = new StreamWriter((fileDestination + elementName + ".txt"), true))
{
while (myReaderPassed.Read())
{
foreach (string testCase in testCasesList)
{
if (myReaderPassed[elementName].ToString() == testCase)
{
{
resultWriter.WriteLine("Field: " + elementName.ToString());
resultWriter.WriteLine("Test Case: " + testCasesList[counter]);
resultWriter.WriteLine("Passed:" + "Yes");
resultWriter.WriteLine("Test: " + cmd.ExecuteScalar());
}
}
}
counter++;
}
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
在这一行: object query = cmd.ExecuteScalar(); 查询始终显示为空。如果我不通过任何参数,该语句是否完美?我错过了什么?