在剛開始的時候,請允許我向所有來自StackOverFlow和所有用戶的人表達我的問候和敬意。你們幫助像我們這樣的人做的很棒。謝謝。Windows窗體應用程序中的SQLException
現在來我的問題: 我正在構建一個winforms應用程序,用於學習的目的: 1)連接到數據庫並根據查詢返回結果。 2)查詢由以下任一生成: *用戶單擊應用程序的各種控件或 *用戶自己編寫查詢並執行它。 3)結果顯示在結果框中。
我得到「錯誤的語法附近','」 生成的語法是正確的,因爲: 1)我複製生成的查詢並在SQL Management Studio中執行它。它在那裏執行完美無瑕。 2)我嘗試手動編寫查詢,然後執行它,但它仍然拋出相同的執行。
請注意,在appliaction開發的第一階段,它只有三個控件,一個DataGridView控件,一個TextBox控件和一個Button控件。在那個階段,我成功地產生了結果並顯示出來。 我添加了應用程序停止工作的額外功能之後。我不明白爲什麼。我使用的是我在開發的第一階段使用的相同代碼,事實上它是相同的應用程序。因此,我用前面提到的三個基本控件創建了另一個應用程序,並複製了主應用程序生成的查詢,然後在第二個應用程序中執行它。它在那裏工作。
在當前階段,如果我使用生成的查詢或者我自己編寫查詢然後執行它,則應用程序將引發相同的執行選項。 這是我的代碼。當用戶點擊「Execute」按鈕時,調用LoadData方法。
try
{
string ConnectString = "Data Source=(local); Initial Catalog=AdventureWorks2008;User ID=; Password=;Integrated Security=SSPI";
ConnectionObj.ConnectionString = ConnectString;//ConnectionObj is SQLConnection object defined in the same class as Loaddata()
ConnectionObj.Open();
ColumnNames = string.Empty;//ColumnNames is a String type defined in the same class as Loaddata()
foreach (string Name in ColumnNamesCheckedListBox.CheckedItems)//ColumnNamesCheckedListBox is a CheckedListBox control which lets user select ColumnNames from the corresponding table
{
ColumnNames = ColumnNames + Name + ", ";
}
int Length = ColumnNames.Length;
Length = Length - 2;//To remove the extra "," and "<space>" at the end of ColumnNames
string TempQuery = ColumnNames.Remove(length);
//User may use the query formed by the application or she may write her own query, hence using "string PossibleQuery"
//SelectQueryDropDownList is a ComboBox control holding the items "Select" "Update" "Insert" and "Delete". Used to generate the DML clause in the query
//Database_TreeView is a TreeView control that holds all the table names in the database. This is used to generate the "FROM" clause in the query
//QueryBox is a TextBox control that displays the generated query and if required lets the user write her own query manually
string PossibleQuery = SelectQueryDropDownList.SelectedItem.ToString() + TempQuery + " From " + Database_TreeView.SelectedNode.Name.ToString();
QueryBox.TempQuery = PossibleQuery;
string FinalQuery = QueryBox.Text; //incase the user modified the query at QueryBox manually
SqlDataAdapter DA = new SqlDataAdapter(FinalQuery, ConnectString);
SqlCommandBuilder CommandBuilder = new SqlCommandBuilder(DA);
DataTable Table = new System.Data.DataTable();
//EXCEPTION OCCURS AT THIS STATEMENT
DA.Fill(Table);
BindingSource Source = new BindingSource();
Source.DataSource = Table;
ResultBox.DataSource = Source;//ResultBox is a DataGridView control to display the results of the the query after execution (if any)
}
catch (Exception e)
{
MessageBox.Show(e.Message+"\nPlease try again","Error",MessageBoxButtons.OK);
}
finally
{
ConnectionObj.Close();
}
以下是截圖: 這是查詢產生的我的應用程序。 http://i60.photobucket.com/albums/h31/spiderclaws/Stack%20Over%20Flow/1ScreenShot2013-04-23at54453PM.png
請幫我一把。感謝名單。 Regards, Gogol Prasad ([email protected]) P.S.-請原諒我寫這麼長的帖子。我希望每個人都清楚這一點。
[編輯] 這裏是要求信息: 請注意,我查詢 Person.BusinessEntityContact按照截圖...
FinalQuery=Select BusinessEntityID, PersonID, ContactTypeID From Person.BusinessEntityContact
StackTrace info:
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
at System.Data.SqlClient.SqlDataReader.get_MetaData()
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)
at ADOBasicsWinFormsApp.Form1.loaddata() in C:\Users\Gogol\Documents\Visual Studio 2010\Projects\ADOBasicsConsoleApp\ADOBasicsWinFormsApp\Form1.cs:line 684
請發佈生成的查詢。 – Rohrbs 2013-04-23 14:09:22
什麼是例外?你能發佈堆棧跟蹤或例外文本嗎? – 2013-04-23 14:10:34
異常說「不正確的語法」,所以發佈FinalQuery變量的內容,你馬上得到答案 – Steve 2013-04-23 14:18:54