Microsoft Office Tutorials and References
In Depth Information
Populating Microsoft Office InfoPath with Microsoft SQL Server 2008
6. Add the following namespaces in the formcode.cs file:
using System.Data;
using System.Data.SqlClient;
7. The following code is used to connect to the Microsoft SQL Server database
and populate the fields with data from the Microsoft SQL server:
public void FormEvents_Loading(object sender, LoadingEventArgs e)
{
// Initializing SqlConnection class.
SqlConnection SQLDatabaseConnection = new SqlConnection();
// Passing ConnectionString property value to SqlConnection class
SQLDatabaseConnection.ConnectionString = "Data Source=WINNER;
Initial Catalog=PacktPub;Integrated Security=True";
SQLDatabaseConnection.Open();
// Initializing SqlCommand class
SqlCommand SQLDatabaseCommand = new SqlCommand();
// Passing the connection information
SQLDatabaseCommand.Connection = SQLDatabaseConnection;
// Setting the command type
SQLDatabaseCommand.CommandType = CommandType.Text;
// SQL Query
SQLDatabaseCommand.CommandText = "select * from Books";
// Initializing SqlDataAdapter Class
SqlDataAdapter SQLDatabaseDataAdapater = new
SqlDataAdapter();
SQLDatabaseDataAdapater.SelectCommand = SQLDatabaseCommand;
// Initializing DataSet Class
DataSet SQLDatabaseDataSet = new DataSet();
SQLDatabaseDataAdapater.Fill(SQLDatabaseDataSet);
// Reading data from dataset
string strValue1 = SQLDatabaseDataSet.Tables[0].Rows[0][2].
ToString();
string strValue2 = SQLDatabaseDataSet.Tables[0].Rows[0][1].
ToString();
// Assigning to the control
XPathNavigator xNode1 = MainDataSource.CreateNavigator().
SelectSingleNode("/my:myFields/my:field1",
NamespaceManager);
xNode1.SetValue(strValue1);
// Assigning to the control
XPathNavigator xNode2 = MainDataSource.CreateNavigator().
SelectSingleNode("/my:myFields/my:field2",
NamespaceManager);
xNode2.SetValue(strValue2);
}
 
Search JabSto ::




Custom Search