我正在尝试运行控制台应用程序(连接并读取SQL),但在'。'附近收到错误语法错误。
static void Main(string[] args)
{
string connstring = @"Data Source=Jama-Dharma\SQLEXPRESS;Initial Catalog=Cars;Integrated Security=True";
SqlConnection conn = new SqlConnection(connstring);
using (conn)
{
string query = "SELECT c.Name, c.Model c.Year FROM CarsCatalog c";
SqlCommand command = new SqlCommand(query, conn);
conn.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(string.Format("{0} {1} {2}", reader.GetString(0), reader.GetString(1), reader.GetInt32(2)));
}
conn.Close();
}
}
答案 0 :(得分:3)
将c.Model c.Year
更改为c.Model, c.Year
答案 1 :(得分:3)
你在这里错过了一个逗号:
string query = "SELECT c.Name, c.Model, c.Year FROM CarsCatalog c";
(在c.Model之后)