Download Logo

Seed SQLite

Required NuGet Packages Create .ipynb file 1 2 3 #r "nuget: Faker.Net, 2.0.163" #r "nuget:SkiaSharp, 3.116.1" #r "nuget: System.Data.SQLite.Core, 1.0.119" Importing namespace 1 2 3 4 5 6 7 using Faker; using System.IO; using SkiaSharp; using System; using System.Collections.Generic; using System.Reflection; using System.Data.SQLite; Create Class 1 2 3 4 5 6 7 8 9 10 11 public class Book { public int Id { get; set; } public string Title { get; set; } public string Author { get; set; } public byte[] Image { get; set; } public decimal Price { get; set; } public string Description { get; set; } public DateTime PublishDate { get; set; } public string Category { get; set; } } Bunch of Color for random background color 1 2 3 4 5 6 7 8 9 10 11 12 var listOfColor = new List<SKColor> { SKColor.Parse("#86B6F6"), SKColor.Parse("#176B87"), SKColor.Parse("#00A9FF"), SKColor.Parse("#FF90BC"), SKColor.Parse("#8ACDD7"), SKColor.Parse("#F2AFEF"), SKColor.Parse("#C499F3"), SKColor.Parse("#33186B"), }; Sqlite Database File Location 1 var connectionString = @"Data Source=C:\Users\91746\source\repos\Shopping\Shopping\BookStore.db"; This will Generate Data for Cover Image Dynamically 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 byte[] Generate<T>(T book) { int width = 480; int height = 540; int marginY = -10; int marginX = -10; string mainText =Faker.Name.First(); //book.Title; string subText = Faker.Name.Last(); string backGroundColor =listOfColor[Faker.RandomNumber.Next(0,listOfColor.Count()-1)].ToString(); string textColor = "#ffffff"; string boderColor = "#ffffff"; SKBitmap bmp = new(width, height); SKCanvas canvas = new(bmp); canvas.Clear(SKColor.Parse(backGroundColor)); using (var paint = new SKPaint()) { paint.TextSize = width/ 10.0f; paint.IsAntialias = true; paint.Color = SKColor.Parse(textColor); paint.IsStroke = false; paint.StrokeWidth = 3; paint.TextAlign = SKTextAlign.Center; canvas.DrawText(mainText, width / 2f, height / 2f, paint); paint.TextSize = width/ 25.0f; paint.TextAlign = SKTextAlign.Right; canvas.DrawText(subText, width+marginX, height+marginY, paint); paint.TextSize = width/ 20.0f; paint.IsStroke = true; paint.TextAlign = SKTextAlign.Center; paint.Color = SKColor.Parse(textColor); } //SKFileWStream fs = new($"Images/{book.Title}.jpg"); //bmp.Encode(fs, SKEncodedImageFormat.Jpeg, quality: 50); bmp.Encode(SKEncodedImageFormat.Jpeg,100); using (MemoryStream ms = new MemoryStream()) { bmp.Encode(ms, SKEncodedImageFormat.Jpeg, 100); return ms.ToArray(); } return bmp.Bytes; } Using Reflection For Auto filling Data To Proprties 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 T GetObjectOf<T>() { Type objectType = typeof(T); object objectObject = Activator.CreateInstance(objectType); // Get the properties of the Book class PropertyInfo[] properties = objectType.GetProperties(); // Use Faker.NET to populate the properties dynamically foreach (var property in properties) { // Skip the 'Id' property as it's usually auto-generated if (property.Name == "Id") continue; // Create fake data based on the property type if (property.PropertyType == typeof(string)) { property.SetValue(objectObject, Faker.Name.FullName()); } else if (property.PropertyType == typeof(int)) { // Assign a random integer property.SetValue(objectObject, Faker.RandomNumber.Next()); } else if (property.PropertyType == typeof(decimal)) { // Assign a random decimal value property.SetValue(objectObject, (decimal)(Faker.RandomNumber.Next(01,1000) )); } else if (property.PropertyType == typeof(DateTime)) { // Assign a random past date property.SetValue(objectObject, DateTime.Now.AddMonths(Faker.RandomNumber.Next(1,100))); } else if (property.PropertyType == typeof(byte[])) { // Assign a random byte array (representing an image or file) property.SetValue(objectObject, Generate((T)objectObject)); } else if (property.PropertyType == typeof(System.Enum)) { // For enum types, assign a random enum value if the property is of enum type Array enumValues = property.PropertyType.GetEnumValues(); var randomEnumValue = enumValues.GetValue(Faker.RandomNumber.Next(0, enumValues.Length)); property.SetValue(objectObject, randomEnumValue); } } return (T)objectObject; } Generate Bunch Of Book Will bw used to insert to database 1 2 3 4 5 6 7 8 9 10 List<T> GeBook<T>() { var ls = new List<T>(); for(var i=0;i<50;i++) { var newBook = GetObjectOf<T>(); ls.Add(newBook); } return ls; } Dynamically Creating SqlQuery string Using Reflection 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 public void InsertBook<T>(T entity, string queryString) { string insertQuery = queryString; try { using (SQLiteConnection conn = new SQLiteConnection(connectionString)) { try { conn.Open(); using (SQLiteCommand cmd = new SQLiteCommand(insertQuery, conn)) { Type type = typeof(T); // Get all properties of the Book class using Reflection PropertyInfo[] properties = type.GetProperties(); foreach (var property in properties) { // Get the name of the property string propertyName = property.Name; object propertyValue = property.GetValue(entity); cmd.Parameters.AddWithValue($"@{propertyName}", propertyValue); } var num = cmd.ExecuteNonQuery(); } } catch(Exception ex) { Console.WriteLine(ex.Message); } } } catch(Exception ex) { Console.WriteLine(ex.Message); } } Here data is inserted to database 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 string InsertCommandStringGenerator<T>() { Type bookType = typeof(T); // Get all the properties of the Book class PropertyInfo[] properties = bookType.GetProperties(); // Initialize StringBuilder to construct the SQL query StringBuilder insertQuery = new StringBuilder(); // Start building the SQL query insertQuery.AppendLine("INSERT INTO Books ("); // Loop through the properties to add column names for (int i = 0; i < properties.Length; i++) { if(properties[i].Name.ToLower()=="id") continue; insertQuery.Append(properties[i].Name); if (i < properties.Length - 1) { insertQuery.Append(", "); } } insertQuery.AppendLine(") VALUES ("); // Loop through the properties again to add parameter placeholders for (int i = 0; i < properties.Length; i++) { if(properties[i].Name.ToLower()=="id") continue; insertQuery.Append("@"); insertQuery.Append(properties[i].Name); if (i < properties.Length - 1) { insertQuery.Append(", "); } } insertQuery.AppendLine(");"); return insertQuery.ToString(); } Final Command To populate all above 1 2 3 4 5 var queryString = InsertCommandStringGenerator<Book>(); foreach(var item in GeBook<Book>()) { InsertBook(item,queryString); }

January 1, 2025 · 5 min · 937 words · PrashantUnity
Download Logo

Spot-check: are you ready?

Q1. What is C#? What is the difference between C# and .NET? C# (C-Sharp) is a modern, object-oriented programming language developed by Microsoft. It is part of the .NET framework and is used for developing a wide range of applications, from web and mobile apps to desktop applications. .NET is a framework developed by Microsoft that provides a comprehensive platform for building and running applications. It includes a runtime environment (CLR), a large class library, and support for various programming languages, including C#, VB.NET, and F#. ...

August 3, 2024 · 127 min · 26990 words · PrashantUnity
Cover Page

Tesseract OCR

Unlocking the Power of Optical Character Recognition with Tesseract In Optical Character Recognition (OCR), Tesseract OCR stands out as a leading open-source library. Renowned for its extensive language support and ability to handle various image formats, Tesseract is a go-to choice for developers seeking to integrate text recognition capabilities into their applications. Key Features Supported Formats Tesseract is adept at recognizing text from a multitude of image formats, including JPG, PNG, BMP, and more. This flexibility ensures it can be seamlessly integrated into diverse projects with varying image input requirements. ...

June 15, 2024 · 4 min · 689 words · PrashantUnity
Cover Page

Windows Media OCR

Windows Media OCR Windows Media OCR is a powerful tool for extracting text from images, derived from Microsoft’s popular PowerToys repository. This tool leverages Windows’ built-in capabilities, making text recognition efficient and seamless for Windows 10 and above. In this post/Article, I’ll show you how to get started with Windows Media OCR and integrate it into your projects. Getting Started First, you need to clone the repository to your local machine. Use the following command: ...

June 15, 2024 · 2 min · 339 words · PrashantUnity
Download Logo

Install SDK & IDE (Ch. 1)

For PC/Mac/Linux Users Install the Software Development Kit (SDK) Download the official SDK from here. 1 https://dotnet.microsoft.com/en-us/download/visual-studio-sdks Choose the appropriate SDK based on your operating system. Follow the on-screen instructions to complete the installation. Download an Integrated Development Environment (IDE) or Code Editor of Your Choice Visual Studio Community Version: Download it here and click on “Free Download”. 1 https://visualstudio.microsoft.com/downloads/ Visual Studio Code: Download it here. 1 https://code.visualstudio.com/?wt.mc_id=vscom_downloads JetBrains Rider: Download it here. 1 https://www.jetbrains.com/rider/ Many more IDEs are available, like Eclipse, etc. Installation Instructions Open the downloaded file. Follow the on-screen instructions to complete the installation. For Users Without a PC or Those Who Don’t Want to Download Software Use an Online IDE SharpLab Paiza.io Dotnet Fiddle Programiz One Compiler W3Schools OnlineGDB For Visual Studio Code Users Install the Polyglot extension for enhanced language support.

June 15, 2024 · 1 min · 136 words · PrashantUnity
Download Logo

First app in VS (Ch. 2)

Create Your First C# Console Application in Visual Studio Follow these steps to create and run a simple C# console application in Visual Studio. Step 1: Create a New Project Open Visual Studio and click on Create a New Project. Step 2: Choose the Console App Template In the search bar, type Console. Select Console App (C#) from the results. Click Next at the bottom right of the window. ...

June 17, 2024 · 2 min · 248 words · PrashantUnity
Download Logo

First app with CLI (Ch. 2)

Create a C# Console Application Using the CLI Follow these steps to create and run a simple C# console application using the command line. Step 1: Open Terminal and Create a New Project Open your terminal and type the following command to create a new console application project named “HelloWorld”: 1 dotnet new console -o HelloWorld Step 2: Confirm Project Creation After pressing Enter, the command will generate a new project. You should see a confirmation message similar to the image below. ...

June 17, 2024 · 2 min · 256 words · PrashantUnity
Download Logo

First notebook app (Ch. 2)

Using Polyglot Notebook in VS Code Step-by-Step Guide 1. Install Polyglot Notebook Open VS Code. Press Control + Shift + X to open the Extensions view. Search for “Polyglot Notebook” or directly open the URL Polyglot Notebook Extension. Look for the extension logo as shown below and install it. 2. Create a New File Create a new file named HelloWorld.ipynb as shown in the image below. Double-click to open the file. ...

June 17, 2024 · 1 min · 150 words · PrashantUnity
Download Logo

C# basics (Ch. 3)

Comments in C# Comments are essential for making your code understandable. They can explain what your code does, which is helpful for anyone reading it (including yourself). Inline Comments: Use // for single-line comments. 1 // This is an inline comment. Multi-line Comments: Use /* */ for comments that span multiple lines. 1 2 3 4 /* This is a multi-line comment. It spans multiple lines. */ Semicolons In C#, every statement ends with a semicolon (;). This tells the compiler where a statement ends. ...

June 16, 2024 · 3 min · 461 words · PrashantUnity
Download Logo

Math in C# (Ch. 4)

C# Basic Operations and Type Conversions Basic Operations In C#, you can perform various arithmetic operations using standard operators. Here are some examples: 1 2 3 4 5 6 int additionResult = 10 + 5; // result will be 15 int subtractionResult = 20 - 8; // result will be 12 int multiplicationResult = 7 * 4; // result will be 28 int divisionResult = 15 / 3; // result will be 5 int remainder = 17 % 5; // remainder will be 2 float floatDivisionResult = 15f / 4f; // result will be 3.75 Understanding Assignment in Programming In general mathematics, equality signifies that the expression on the left side of the = is equivalent to the expression on the right side: ...

June 18, 2024 · 2 min · 417 words · PrashantUnity