[英]How to read an ADOQueryConnection with C# in Infopath 2013
I am currently creating my first InfoPath Form with C# code on the back end. 我目前正在使用后端的C#代码创建我的第一个InfoPath表单。 It is connecting to a SQL Server DB as well.
它也正在连接到SQL Server数据库。 When the form opens I want it to connect to our SQL Server DB and pull in the user's name and title automatically.
表单打开时,我希望它连接到我们的SQL Server数据库并自动输入用户的名称和标题。 However, I'm rather stuck on what seems like a very simple task.
但是,我宁愿停留在看起来很简单的任务上。
How do I read the data from an ADO Connection after I've executed the query? 执行查询后,如何从ADO连接读取数据? Below is my code.
下面是我的代码。 This seems so simple but I can't seem to figure it out.
这似乎很简单,但我似乎无法弄清楚。
AD_Mirror contains the ADO Connection to the DB and the appropriate table. AD_Mirror包含与数据库的ADO连接和相应的表。 The CustomQuery is my attempt to filter this down by the user that is opening the form.
CustomQuery是我尝试通过打开表单的用户对此进行过滤。
I then execute it in theory grabbing the user information I need. 然后,我将在理论上执行它,以获取所需的用户信息。 Then I am unsure as to how to proceed.
然后我不确定如何进行。 I've searched all over for an answer, so any help would be greatly appreciated.
我一直在寻找答案,因此我们将不胜感激。
//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();
So I had to separate out the connection from the SQL Query. 因此,我不得不从SQL查询中分离出连接。 Then pull in a datatable to pull the data in.
然后拉入数据表以拉入数据。
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();
My query has First Name in Column 9 (hence the above ItemArray of 9). 我的查询在第9列中具有“名字”(因此上面的ItemArray为9)。 You can set it tup to loop through this if you need to but I didn't.
如果需要,可以将其设置为循环播放,但我没有。
Thanks 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.