chaseernst
posted this
16 October 2018
I ended up getting an answer to the question based off of Ola's private message. Below is my script in C#.
SqlConnection sqlConnection = new SqlConnection();
sqlConnection.ConnectionString = "your connection string"
SqlDataReader reader;
sqlConnection.Open();
IDataSet dataSet = ruleContext.DataSet("customersDataSet"); //ruleContext is the variable that is passed through the event handler. It is Interface type IRuleContext.
List<string> dataList = new List<string>();
//Setup the connection parameters
SqlCommand cmd = new SqlCommand();
cmd.Connection = sqlConnection;
cmd.CommandText = "your sql query";
try
{
reader = cmd.ExecuteReader();
IDataSetRecord rec;
while (reader.Read())
{
dataList.Add(reader.GetString(0));
}
sqlConnection.Close();
for(int i = 0; i <= dataList.ToArray().Count() - 1; i++)
{
rec = dataSet.CreateRecord();
rec.AddValue("Customer", dataList[i]); //need to specify the column name in the data set here.
dataSet.AddRecord(rec);
}
}
catch (Exception ex)
{
sqlConnection.Close();
FCTools.ShowMessage(ex.Message);
}
I have this code running through a script applied to a specific field on the document definition. Although this code will update the data set properly, there are a few issues I have found. First, for whatever reason every script in the document definition will make this script execute, even though it is only specified on one control's rule. Second, the iteration of dataSet.AddRecord(rec) takes an extraordinary amount of time. To process 150 records it takes longer than 1 minute to run the for statement.