Im trying to store my sql statement in .sql files in a resource folder,
I need to pass in 2 values to the sql command but im having difficulty doing this
my sql is as follows:
SELECT * FROM claim_header WHERE @columnname = @testcase;
My class calling it:
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());
}
}
At this line: object query = cmd.ExecuteScalar(); query always shows up as null. If I dont pass any paramenters the statement works perfectly? What am I missing?