Problems accessing SQL Server

D

Dafydd Giddins

Hi

I set up a new instance of SQL Server using the developer
edition CD and set up a datbase called AMA, table called
user with two fields called userid (the primary key) and
username (a string). Wheni run the code i get the
following error message

Incorrect Syntax near the keyword 'user'.

What am i missing?
Regards

Dafydd

Code is below


SqlConnection conn = new
SqlConnection
("server=localhost;database=AMA;uid=sa;pwd=password");
try
{
conn.Open ();
SqlCommand cmd = new
SqlCommand ("select * from user", conn);
SqlDataReader reader =
cmd.ExecuteReader ();
while (reader.Read ())
Console.WriteLine
(reader["username"]);
}
catch (SqlException ex)
{
Console.WriteLine
("Error:" + ex.Message);
}
finally
{
conn.Close ();
}
 
K

Kumar Gaurav Khanna [.NET MVP]

Hi!

"User" is a keyword of the SQL Server T-SQL. Have a look at this link:

http://msdn.microsoft.com/library/en-us/tsqlref/ts_ra-rz_9oj7.asp?frame=true

You will need to change the name of your table.

Regards,
Kumar Gaurav Khanna

--
----------------------------------------------------------------------------
----------
Microsoft MVP - .NET, MCSE Windows 2000/NT4, MCP+I
WinToolZone - Spelunking Microsoft Technologies
http://www.wintoolzone.com/
OpSupport - Spelunking Rotor
http://opsupport.sscli.net/
 
M

Martin Dew

Kumar Gaurav Khanna said:
Hi!

"User" is a keyword of the SQL Server T-SQL. Have a look at this link:

http://msdn.microsoft.com/library/en-us/tsqlref/ts_ra-rz_9oj7.asp?frame=true

You will need to change the name of your table.

Regards,
Kumar Gaurav Khanna

--
-------------------------------------------------------------------------- --
----------
Microsoft MVP - .NET, MCSE Windows 2000/NT4, MCP+I
WinToolZone - Spelunking Microsoft Technologies
http://www.wintoolzone.com/
OpSupport - Spelunking Rotor
http://opsupport.sscli.net/
-------------------------------------------------------------------------- --
----------

Dafydd Giddins said:
Hi

I set up a new instance of SQL Server using the developer
edition CD and set up a datbase called AMA, table called
user with two fields called userid (the primary key) and
username (a string). Wheni run the code i get the
following error message

Incorrect Syntax near the keyword 'user'.

What am i missing?
Regards

Dafydd

Code is below


SqlConnection conn = new
SqlConnection
("server=localhost;database=AMA;uid=sa;pwd=password");
try
{
conn.Open ();
SqlCommand cmd = new
SqlCommand ("select * from user", conn);
SqlDataReader reader =
cmd.ExecuteReader ();
while (reader.Read ())
Console.WriteLine
(reader["username"]);
}
catch (SqlException ex)
{
Console.WriteLine
("Error:" + ex.Message);
}
finally
{
conn.Close ();
}

or use the [user] syntax when dealing with objects that are named using
reserved words.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

SqlConnection 2
Accesing SQL View form C# asp.net 1
Error while running asp.net 3
OOP advice needed 13
"Invalid attempt to read when no data is present" 4
C# database access 5
blob 9
stored procedure have no results 6

Top