简体   繁体   English

读取SQL Server数据库

[英]Reading SQL Server database

I need to read data from a SQL Server database. 我需要从SQL Server数据库读取数据。 Something doesn't work with syntax of the table, which has Id as a primary key plus several items. 该表的语法无法正常工作,该表具有Id作为主键以及一些项目。

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 没有错误消息:ex.Message

Wrong syntax close to 'dbo.Table'. 语法接近'dbo.Table'的语法错误。

Many thanks for your ideas. 非常感谢您的想法。

You're missing a space in your SQL and using the brackets incorrectly. 您在SQL中缺少空格,并且错误地使用了方括号。 Columns need to be separated by commas. 列需要用逗号分隔。 The line should be: 该行应为:

string strSQL = "SELECT Id, Item FROM [dbo].[Table]";

在ur查询中,在Item之后留一个空格,然后将dbo.table与[dbo]。[table]分开,例如:

string strSQL = "SELECT Id, Item " + "FROM [dbo].[Table]";

Item和From之间没有空格,将您的strSql行替换为以下内容:

string strSQL = "SELECT Id Item " + "FROM [dbo.Table]";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM