为什么在尝试从SQL表中进行SELECT时会出现错误?

本文关键字:SELECT 错误 SQL 为什么 | 更新日期: 2023-09-27 18:02:37

我在c#中有以下代码来检索表列:

    protected void Page_Load(object sender, EventArgs e)
    {
using (OleDbConnection connection = new OleDbConnection("Provider=MSDataShape;Data Provider=SQLOLEDB;" +
  "Data Source=wmg-erp-db;Initial Catalog=DP;User ID=zh;Password=zhas"))
{
OleDbDataAdapter adapter = new OleDbDataAdapter("SHAPE {SELECT FROM [DP].[dbo].[BT]} ", connection);
DataSet customers = new DataSet();
adapter.Fill(customers, "Customers");
}
    }

我得到以下错误:

Exception Details: System.Data.OleDb.OleDbException: Incorrect syntax near the keyword 'FROM'.
Source Error:

Line 35: 
Line 36: DataSet customers = new DataSet();
Line 37: adapter.Fill(customers, "Customers");
Line 38: }
Line 39:    }

Source File: c:'Webserver'WEXEC'Booking'booking.aspx.cs    Line: 37
Stack Trace:

[OleDbException (0x80040e14): Incorrect syntax near the keyword 'FROM'.]
   System.Data.OleDb.OleDbDataReader.ProcessResults(OleDbHResult hr) +60
   System.Data.OleDb.OleDbDataReader.NextResult() +630
   System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) +546
   System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior) +264
   System.Data.OleDb.OleDbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior) +9
   System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +325
   System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +420
   System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable) +280
   booking.Page_Load(Object sender, EventArgs e) in c:'Webserver'EXEC'Booking'booking.aspx.cs:37
   System.Web.UI.Control.LoadRecursive() +71
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3178

如何解决这个问题?

为什么在尝试从SQL表中进行SELECT时会出现错误?

您在查询语句中遗漏了字段/列名。在查询

中提及某些特定的列名(OR) *

此处行

new OleDbDataAdapter("SHAPE {SELECT FROM [DP].[dbo].[BT]} "
应该

new OleDbDataAdapter("SHAPE {SELECT somecolumn FROM [DP].[dbo].[BT]} "

您需要指定要选择的内容。正确的查询应该是SELECT * FROM [DP].[dbo].[BT]该查询将返回所有记录。

SELECT * FROM, *失踪

指定列名,或为所有列指定*

引用