Retrieving the data from SQL Server and displaying it in FlowDocument
I did this small demonstration in one of my presentations and thought I’ll post this very simple code snippet. I’m reading the data from Address table in AdventureWorks database and displaying it in a flow document here goes the code
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
CreateFlowDocument();
}
public void CreateFlowDocument()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=SQLDB;Initial Catalog=AdventureWorks;Integrated Security=True";
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Select AddressLine1,City from Person.Address";
cmd.Connection = con;
SqlDataReader rdr=cmd.ExecuteReader() ;
StringBuilder strbldr = new StringBuilder();
while (rdr.Read())
{
strbldr.AppendLine(rdr[0].ToString() + rdr[1].ToString()+"nICE"); ;
}
Paragraph myParagraph = new Paragraph();
myParagraph.Inlines.Add(new Bold(new Run("Some bold text in the paragraph.")));
myParagraph.Inlines.Add(new Run(strbldr.ToString() ));
FlowDocument myFlowDocument = new FlowDocument();
myFlowDocument.Blocks.Add(myParagraph);
SolidColorBrush objBrush=new SolidColorBrush();
objBrush.Color =Colors.Blue ;
myFlowDocument.Background = objBrush;
FlowDocumentReader myFlowDocumentReader = new FlowDocumentReader();
myFlowDocumentReader.Document = myFlowDocument;
this.Content = myFlowDocumentReader;
}
}
}