簡體   English   中英

如何在Infopath 2013中使用C#讀取ADOQueryConnection

[英]How to read an ADOQueryConnection with C# in Infopath 2013

我目前正在使用后端的C#代碼創建我的第一個InfoPath表單。 它也正在連接到SQL Server數據庫。 表單打開時,我希望它連接到我們的SQL Server數據庫並自動輸入用戶的名稱和標題。 但是,我寧願停留在看起來很簡單的任務上。

執行查詢后,如何從ADO連接讀取數據? 下面是我的代碼。 這似乎很簡單,但我似乎無法弄清楚。

AD_Mirror包含與數據庫的ADO連接和相應的表。 CustomQuery是我嘗試通過打開表單的用戶對此進行過濾。

然后,我將在理論上執行它,以獲取所需的用戶信息。 然后我不確定如何進行。 我一直在尋找答案,因此我們將不勝感激。

       //ADO Attempt
        AdoQueryConnection connection =
       (AdoQueryConnection)DataConnections["AD_Mirror"];

        string customQuery = " where SamAccountName = '" + userName + "'";
        string origCommand = connection.Command.ToString();
        //set the query to use the custom command
        connection.Command = origCommand + customQuery;
        //annnnnnddddd, now we run the query using our shiny new command           
         connection.Execute();

因此,我不得不從SQL查詢中分離出連接。 然后拉入數據表以拉入數據。

    string queryString = origCommand + customQuery;  //Pull in Query
    string conn = connection.Connection;  //Grab Connection

                     using (OleDbConnection connection1 = new OleDbConnection(conn))
             {
                 OleDbCommand command = new OleDbCommand(queryString, connection1);
                 command.CommandType = CommandType.Text;

                 try
                 {
                     connection1.Open();
                     OleDbDataAdapter adapter = new OleDbDataAdapter(command);
                     adapter.Fill(oSet);

                             string firstName = (oSet.Tables[0].Rows[0].ItemArray[9]).ToString();
                     firstName = firstName.Trim();

我的查詢在第9列中具有“名字”(因此上面的ItemArray為9)。 如果需要,可以將其設置為循環播放,但我沒有。

謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM