I am currently creating my first InfoPath Form with C# code on the back end. It is connecting to a SQL Server DB as well. When the form opens I want it to connect to our SQL Server DB and pull in the user's name and title automatically. 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? 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. The CustomQuery is my attempt to filter this down by the user that is opening the form.
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. 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). You can set it tup to loop through this if you need to but I didn't.
Thanks
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.