I need to read data from a SQL Server database. Something doesn't work with syntax of the table, which has Id as a primary key plus several items.
SqlConnection CON = new SqlConnection("Data Source = pc\\sqlexpress; Initial Catalog = dccDB; Integrated Security = True");
string strSQL = "SELECT Id Item" + "FROM [dbo.Table]";
SqlCommand cmd = new SqlCommand(strSQL, CON);
{
CON.Open();
MessageBox.Show("SQL DataBase dccDB.dbo is connected");
SqlDataReader reader = cmd.ExecuteReader(); (ERROR !!!)
while ( reader.Read() )
{
MessageBox.Show( reader["Id"].ToString(), reader["Item"].ToString());
}
reader.Close();
CON.Close();
}
There is a error message: ex.Message
Wrong syntax close to 'dbo.Table'.
Many thanks for your ideas.
答案 0 :(得分:4)
You're missing a space in your SQL and using the brackets incorrectly. Columns need to be separated by commas. The line should be:
string strSQL = "SELECT Id, Item FROM [dbo].[Table]";
答案 1 :(得分:2)
Have a space after Item in ur query, and separate dbo.table to [dbo].[table] like:
string strSQL = "SELECT Id, Item " + "FROM [dbo].[Table]";
答案 2 :(得分:1)
Item和From之间没有空格,用以下内容替换你的strSql行:
string strSQL = "SELECT Id Item " + "FROM [dbo.Table]";