[英]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.