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

时间:2014-05-09 12:58:47

标签: c# sql sql-server

我在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

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:3)

您错过了在查询语句中提及字段/列名称。在查询中提到一些特定的列名(OR)*

在这里排队

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

应该是

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

答案 1 :(得分:2)

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

答案 2 :(得分:2)

SELECT * FROM*失踪。

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

Refer