2
Answers

error in reading table from a sql database C#

soroush parsi

soroush parsi

7y
207
1
Hello
I am beginner in sql and now I want to read or write to a table in a local database and I get this error:

>An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll Additional information: Incorrect syntax near the keyword 'Table'.

my code is here:

using (SqlConnection con = new SqlConnection(@"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename = c:\users\soroush\documents\visual studio 2015\Projects\databasetest2\databasetest2\Database1.mdf; Integrated Security = True"))
{
con.Open();
string t = "SELECT * From Table";
SqlCommand cmd = new SqlCommand(t, con);
SqlDataReader reader = cmd.ExecuteReader();

while (reader.Read())
{
MessageBox.Show(reader["Id"].ToString() + reader["name"].ToString());
}
con.Close();
}


the error points to this line of my code when i want to write on table:

>SqlDataReader reader = cmd.ExecuteReader();

and I have a database whit one table named "Table" contains two columns; Id(int) and name(nchar10)

how cand i read this table and use the contents in my code?

thank you

Answers (2)