2012-01-27 99 views
1

有沒有辦法從SqlCommand獲取參數集合時CommandType = TextSqlCommand(CommandType文本)檢索參數集合

例如:

string MyDinamicSql = @"SELECT * FROM USERS where USERName = @Param1 and [email protected]"; 
SqlCommand comand = connection.CreateCommand(); 
comand.CommandText = MyDinamicSql; 
comand.CommandType = System.Data.CommandType.Text; 

//Do something for fill comand.Parameters from MyDinamicSql... 

我想辦法讓一個參數採集與[@參數1,@參數2] ...我不知道SQL查詢(它們是動態的),我想獲取我的應用程序的參數,將它們創建爲輸入控件。

我不能這樣做與SqlCommandBuilder.DeriveParameters(command),因爲它只適用於存儲過程。

謝謝!

+0

如何生成SQL字符串?它們是從代碼構建還是來自用戶? – 2012-01-27 18:43:08

+0

他們被寫入沒有地方參數名稱的商店...: - /他們來自用戶,我的應用程序不知道查詢參數。 – 2012-01-27 18:55:41

+0

在delphi中,當它的引擎解析sql時,我有類似FetchParams(在ClientDataSet上)並執行此操作。但在C#中我沒有發現任何喜歡它。 – 2012-01-27 19:07:54

回答

2

否 - 直到SQL Server解析命令,它只是一個字符串。

+0

好吧,有沒有辦法強制sql服務器解析我的命令文本來獲取參數集合? – 2012-01-27 18:41:27

+0

SQL Server甚至不會嘗試,除非你給它參數,對不起:( – egrunin 2012-01-27 21:14:21