Question

I'm trying to do what the title said but I get this error at runtime:

Incorrect syntax near the keyword 'Top'.

string connString = @"server  =.\sqlexpress; Database=BestScores.mdf; trusted_connection=TRUE; AttachDbFileName= D:\Programing\Projects Visual Studio 2008\JigSaw\JigSaw\bin\Debug\BestScores.mdf";
SqlConnection conn = new SqlConnection(connString);
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(@"SELECT * FROM Top", conn);
da.Fill(ds);//Error
Was it helpful?

Solution

Top is a reserved keyword on Transact-SQL. When you want to use it in your sql command, you have to use it with square brackets like [TOP].

SqlDataAdapter da = new SqlDataAdapter(@"SELECT * FROM [Top]", conn);

That's why you are getting

Incorrect syntax near the keyword 'Top'

OTHER TIPS

Try to change the Top to another name, because TOP is a reserved word on SQL. Check this out http://www.w3schools.com/sql/sql_top.asp

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top