Here’s a sample code in C# using the .NET framework to read data from a SQL Server database using System; using System.Data; using System.Data.SqlClient; namespace SqlDatabaseReaderExample { class Program { static void Main(string[] args) { // Define the connection string for the SQL Server database string connectionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=Username;Password=Password;"; // Define the SQL statement to select data from the SQL Server database string sql = "SELECT * FROM Customers"; // Create a new instance of the SqlConnection class using (SqlConnection connection = new SqlConnection(connectionString)) { // Open the connection to the SQL Server database connection.Open(); // Create a new instance of the SqlCommand class using (SqlCommand command = new SqlCommand(sql, connection)) { // Create a new instance of the SqlDataAdapter class using (SqlDataAdapter adapter = new SqlDataAdapter(command)) { // Create a new instance of the DataTable class DataTable table = new DataTable(); // Fill the DataTable with the results of the SQL statement adapter.Fill(table); // Print the results to the console foreach (DataRow row in table.Rows) { Console.WriteLine("{0}t{1}t{2}", row["FirstName"], row["LastName"], row["Email"]); } } } } Console.WriteLine("Data has been read from the SQL Server database."); Console.ReadLine(); } } } In this example, the SqlConnection class is used to create a new instance of the SQL connection to the SQL Server database. The Open method of the SqlConnection object is used to open the connection. The SqlCommand class is used to create a new instance of the SQL command to select data from the SQL Server database. The SqlDataAdapter class is used to create a new instance of the data adapter to fill a DataTable object with the results of the SQL statement. The Fill method of the SqlDataAdapter object is used to fill the DataTable with the results of the SQL statement. Finally, the results are printed to the console using a foreach loop to iterate through the rows of the DataTable.
Read data from Progress database using .NET
Here’s a sample code in C# using the .NET framework to read data from a Progress database using System; using System.Data; using System.Data.Odbc; namespace ProgressDatabaseReaderExample { class Program { static void Main(string[] args) { // Define the connection string for the Progress database string connectionString = "DSN=ProgressDSN;UID=Username;PWD=Password;"; // Define the SQL statement to select data from the Progress database string sql = "SELECT * FROM Customer WHERE CustomerID = ?"; // Define the parameter value for the SQL statement string customerID = "12345"; // Create a new instance of the OdbcConnection class using (OdbcConnection connection = new OdbcConnection(connectionString)) { // Open the connection to the Progress database connection.Open(); // Create a new instance of the OdbcCommand class using (OdbcCommand command = new OdbcCommand(sql, connection)) { // Define the parameter value for the SQL statement command.Parameters.AddWithValue("@CustomerID", customerID); // Create a new instance of the OdbcDataAdapter class using (OdbcDataAdapter adapter = new OdbcDataAdapter(command)) { // Create a new instance of the DataTable class DataTable dataTable = new DataTable(); // Fill the DataTable with the data from the Progress database adapter.Fill(dataTable); // Loop through each row in the DataTable foreach (DataRow row in dataTable.Rows) { // Print the data to the console Console.WriteLine("{0} {1} {2} {3} {4} {5}", row["CustomerID"], row["CustomerName"], row["Address"], row["City"], row["State"], row["Zip"]); } } } } Console.WriteLine("Data has been read from the Progress database."); Console.ReadLine(); } } } In this example, the Odbc library is used to read data from a Progress database. The OdbcConnection class is used to create a new instance of the ODBC connection to the Progress database. The Open method of the OdbcConnection object is used to open the connection. The OdbcCommand class is used to create a new instance of the SQL command to select data from the Progress database. The AddWithValue method of the OdbcParameterCollection object is used to define the parameter value for the SQL statement. The OdbcDataAdapter class is used to create a new instance of the data adapter to fill a DataTable object with the data from the Progress database. Finally, the data in the DataTable object is printed to the console.
Write data to Progress database using .NET
Here’s a sample code in C# using the .NET framework to write data to a Progress database using System; using System.Data; using System.Data.Odbc; namespace ProgressDatabaseWriterExample { class Program { static void Main(string[] args) { // Define the connection string for the Progress database string connectionString = "DSN=ProgressDSN;UID=Username;PWD=Password;"; // Define the SQL statement to insert data into the Progress database string sql = "INSERT INTO Customer (CustomerID, CustomerName, Address, City, State, Zip) VALUES (?, ?, ?, ?, ?, ?)"; // Define the parameter values for the SQL statement string customerID = "12345"; string customerName = "Acme Inc."; string address = "123 Main St"; string city = "Anytown"; string state = "CA"; string zip = "12345"; // Create a new instance of the OdbcConnection class using (OdbcConnection connection = new OdbcConnection(connectionString)) { // Open the connection to the Progress database connection.Open(); // Create a new instance of the OdbcCommand class using (OdbcCommand command = new OdbcCommand(sql, connection)) { // Define the parameter values for the SQL statement command.Parameters.AddWithValue("@CustomerID", customerID); command.Parameters.AddWithValue("@CustomerName", customerName); command.Parameters.AddWithValue("@Address", address); command.Parameters.AddWithValue("@City", city); command.Parameters.AddWithValue("@State", state); command.Parameters.AddWithValue("@Zip", zip); // Execute the SQL statement to insert data into the Progress database int rowsAffected = command.ExecuteNonQuery(); Console.WriteLine("{0} row(s) affected.", rowsAffected); } } Console.WriteLine("Data has been written to the Progress database."); Console.ReadLine(); } } } In this example, the Odbc library is used to write data to a Progress database. The OdbcConnection class is used to create a new instance of the ODBC connection to the Progress database. The Open method of the OdbcConnection object is used to open the connection. The OdbcCommand class is used to create a new instance of the SQL command to insert data into the Progress database. The AddWithValue method of the OdbcParameterCollection object is used to define the parameter values for the SQL statement. Finally, the ExecuteNonQuery method of the OdbcCommand object is used to execute the SQL statement and write data to the Progress database.
Download attachments from Outlook emails using .NET
Here’s a sample code in C# using the .NET framework to download attachments from Outlook using System; using System.IO; using System.Runtime.InteropServices; using Outlook = Microsoft.Office.Interop.Outlook; namespace OutlookAttachmentDownloaderExample { class Program { static void Main(string[] args) { // Define the Outlook application Outlook.Application outlookApp = new Outlook.Application(); // Define the Outlook mail folder and the mail item Outlook.MAPIFolder inbox = outlookApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox); Outlook.MailItem mailItem = inbox.Items[1] as Outlook.MailItem; // Define the attachment download path string downloadPath = @"C:UsersUserNameDocumentsAttachments"; // Loop through each attachment in the mail item foreach (Outlook.Attachment attachment in mailItem.Attachments) { // Define the attachment file name and download path string fileName = attachment.FileName; string filePath = Path.Combine(downloadPath, fileName); // Download the attachment attachment.SaveAsFile(filePath); Console.WriteLine("Attachment {0} has been downloaded to {1}.", fileName, filePath); } Console.WriteLine("Attachments have been downloaded from the Outlook mail item."); Console.ReadLine(); } } } In this example, the Microsoft Office Interop Outlook library is used to download attachments from Outlook. The Application class is used to create a new instance of the Outlook application. The MAPIFolder class is used to define the Outlook mail folder, and the MailItem class is used to define the mail item from which the attachments will be downloaded. The Attachment class is used to loop through each attachment in the mail item. The SaveAsFile method of the Attachment object is used to download the attachment to the specified download path. Finally, the downloaded attachments are printed to the console.
Extract data from a Web page using .NET
here’s a sample code in C# using the .NET framework to extract data from a Wiki web page using System; using System.Net; using HtmlAgilityPack; namespace WikiPageExtractorExample { class Program { static void Main(string[] args) { // Define the Wiki web page URL string url = "https://en.wikipedia.org/wiki/.NET_Framework"; // Create a new instance of the HTML web client WebClient client = new WebClient(); // Download the HTML content of the web page string html = client.DownloadString(url); // Create a new instance of the HTML document HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(html); // Extract the text from the main content of the web page HtmlNode mainContent = doc.DocumentNode.SelectSingleNode("//div[@id='mw-content-text']"); string text = mainContent.InnerText; Console.WriteLine(text); Console.WriteLine("Data has been extracted from the Wiki web page."); Console.ReadLine(); } } } In this example, the HtmlAgilityPack library is used to extract data from the Wiki web page. The WebClient class is used to create a new instance of the HTML web client, which is used to download the HTML content of the web page. The HtmlDocument class is used to create a new instance of the HTML document, which is used to load the HTML content. The SelectSingleNode method of the HtmlNode class is used to select the main content of the web page by its ID attribute. The InnerText property of the HtmlNode object is used to extract the text from the main content of the web page. Finally, the extracted text is printed to the console.
Extract data from PDF using .NET
here’s a sample code in C# using the .NET framework to extract data from a PDF using System; using System.IO; using iTextSharp.text; using iTextSharp.text.pdf; namespace PDFExtractorExample { class Program { static void Main(string[] args) { // Define the PDF file path and name string filePath = @"C:UsersUserNameDocumentsexample.pdf"; // Create a new instance of the PDF reader using (PdfReader reader = new PdfReader(filePath)) { // Extract text from each page of the PDF document for (int i = 1; i <= reader.NumberOfPages; i++) { string text = PdfTextExtractor.GetTextFromPage(reader, i); Console.WriteLine("Page {0}:n{1}", i, text); } } Console.WriteLine("Data has been extracted from the PDF."); Console.ReadLine(); } } } In this example, the iTextSharp library is used to extract text from the PDF. The PdfReader class is used to create a new instance of the PDF reader with the PDF file path and name. The GetTextFromPage method of the PdfTextExtractor class is used to extract text from each page of the PDF document. The text is then printed to the console. Finally, the PdfReader object is disposed of properly using the using statement.
Write data to Word document using .NET
here’s a sample code in C# using the .NET framework to write data to a Word document using System; using Microsoft.Office.Interop.Word; namespace WordDocWriterExample { class Program { static void Main(string[] args) { // Define the Word document path and name string filePath = @"C:UsersUserNameDocumentsexample.docx"; // Define the data to be written to the Word document string data = "Hello, world!"; // Create a new instance of Word application Application wordApp = new Application(); // Open the Word document Document wordDoc = wordApp.Documents.Open(filePath); // Set the cursor position to the end of the document object missing = System.Reflection.Missing.Value; object endOfDoc = "\endofdoc"; Range range = wordDoc.Bookmarks.get_Item(ref endOfDoc).Range; // Write the data to the Word document range.Text = data; // Save and close the Word document wordDoc.Save(); wordDoc.Close(); Console.WriteLine("Data has been written to the Word document."); Console.ReadLine(); } } } In this example, the Application and Document classes from the Microsoft Office Interop Word library are used to create a new instance of Word application and open the Word document. The Range object is used to set the cursor position to the end of the document. The Text property of the Range object is used to write the data to the Word document. The Save and Close methods of the Document object are used to save and close the Word document.
Write data to Access database using .NET
here’s a sample code in C# using the .NET framework to write data to an Access database using System; using System.Data.OleDb; namespace AccessDBWriterExample { class Program { static void Main(string[] args) { // Define the Access database connection string string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:UsersUserNameDocumentsexample.accdb"; // Define the SQL query to insert data into the database string query = "INSERT INTO ExampleTable (Name, Age, Gender) VALUES ('John Smith', 30, 'Male')"; // Open the Access database connection using (OleDbConnection conn = new OleDbConnection(connString)) { // Open the connection conn.Open(); // Create a command object with the SQL query and the connection using (OleDbCommand cmd = new OleDbCommand(query, conn)) { // Execute the SQL query int rowsAffected = cmd.ExecuteNonQuery(); Console.WriteLine("{0} rows affected.", rowsAffected); } } Console.WriteLine("Data has been written to the Access database."); Console.ReadLine(); } } } In this example, the OleDbConnection class is used to establish a connection to the Access database. The connection string includes the provider name and the path to the Access database file. The OleDbCommand class is used to create a command object with the SQL query and the connection. The ExecuteNonQuery method is used to execute the SQL query and return the number of rows affected.
Write data to a TXT/Flat file using .NET
here’s a sample code in C# using the .NET framework to write data to a text or flat file using System; using System.IO; namespace TextWriterExample { class Program { static void Main(string[] args) { // Define the text file path and name string filePath = @"C:UsersUserNameDocumentsexample.txt"; // Define the data to be written to the text file string data = "Hello, world!"; // Open the text file for writing using (StreamWriter sw = new StreamWriter(filePath, true)) { // Write the data to the text file sw.WriteLine(data); } Console.WriteLine("Data has been written to the text file."); Console.ReadLine(); } } } In this example, the StreamWriter class is used to write the data to the text file. The using statement ensures that the StreamWriter object is disposed of properly when it is no longer needed. The WriteLine method is used to write the data to the text file, followed by a new line character to move the cursor to the next line.
Write data to CSV using .NET
here’s a sample code in C# using the .NET framework to write data to a CSV file using System; using System.IO; namespace CSVWriterExample { class Program { static void Main(string[] args) { // Define the CSV file path and name string filePath = @"C:UsersUserNameDocumentsexample.csv"; // Define the data to be written to the CSV file string[] data = {"John Smith", "30", "Male"}; // Open the CSV file for writing using (StreamWriter sw = new StreamWriter(filePath, true)) { // Write the data to the CSV file sw.WriteLine(string.Join(",", data)); } Console.WriteLine("Data has been written to the CSV file."); Console.ReadLine(); } } } In this example, the StreamWriter class is used to write the data to the CSV file. The using statement ensures that the StreamWriter object is disposed of properly when it is no longer needed. The string.Join method is used to join the data elements into a single string separated by commas, which is the standard format for CSV files.