mr.cool mr.cool - 3 months ago 12
MySQL Question

get data from database using c#

iam looking to get the data from database .when i give the filename i want the pageinfo

public string GetPageInfo(string filenames)
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=;Integrated Security=True");

con.Open();

SqlCommand command = new SqlCommand("Select pageinfo from T_Pages where @pagename=filenames", con);

command.Parameters.AddWithValue("@pagename", "filenames");
// int result = command.ExecuteNonQuery();
using (SqlDataReader reader = command.ExecuteReader())
{
if (reader.Read())
{
Console.WriteLine(String.Format("{0}", reader["pageinfo"]));
}
}

con.Close();

}

Answer

public string GetPageInfo(string filenames)

your method asks for a string to be returned at the end of the blocks.

according to your method's flow, the last thing it has to do is to write the result/s in the Console Window

if (reader.Read()) { Console.WriteLine(String.Format("{0}", reader["pageinfo"])); }

you have to choices:

  1. either change public string GetPageInfo(string filenames) to public void GetPageInfo(string filenames); or,

  2. return whatever column it is that you need from the database. that is changing:

if (reader.Read()) { Console.WriteLine(String.Format("{0}", reader["pageinfo"])); }

to

return reader["pageinfo"];