I am writing a method in C# to query a SQL Server Express database from a WCF service. I have to use ADO.NET to do this (then rewrite it with LINQ later on).
The method takes two strings (fname, lname
) then returns a "Health Insurance NO" attribute from the matching record. I want to read this into a list (there are some other attribs to retrieve as well).
The current code returns an empty list. Where am I going wrong?
public List<string> GetPatientInfo(string fname, string lname)
{
string connString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\xxxx\\Documents\\Visual Studio 2010\\Projects\\ADOWebApp\\ADOWebApp\\App_Data\\ADODatabase.mdf;Integrated Security=True;User Instance=True";
SqlConnection conn = new SqlConnection(connString);
string sqlquery = "SELECT Patient.* FROM Patient WHERE ([First Name] = '"+fname+"') AND ([Last Name] = '"+lname+"')";
SqlCommand command = new SqlCommand(sqlquery, conn);
DataTable dt = new DataTable();
List<string> result = new List<string>();
using (conn)
{
conn.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader != null && reader.Read())
{
dt.Load(reader);
result.Add(Convert.ToString(reader["Health Insurance NO"]));
}
}
}
return result;
}