Skip to content

Commit 9a00078

Browse files
committed
Sqlite Addition
1 parent fef4c15 commit 9a00078

File tree

3 files changed

+299
-2
lines changed

3 files changed

+299
-2
lines changed

content/blog/general/sqlite.md

+294
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,294 @@
1+
---
2+
title: "Sqlite Database "
3+
author: "PrashantUnity"
4+
weight: 100
5+
date: 2025-01-01
6+
lastmod: 2025-01-01
7+
dateString: January 2025
8+
description: "Populate Sqlite Data Base Using Poluglot Notebook and Net"
9+
#canonicalURL: "https://canonical.url/to/page"
10+
cover:
11+
image: "sqlite.svg" # image path/url
12+
alt: "Download Logo" # alt text
13+
#caption: "Optical Character Recognition" display caption under cover
14+
15+
tags: [ "NET", "codefrydev", "C sharp", "CFD","Sqlite"]
16+
keywords: [ "NET", "codefrydev", "C sharp", "CFD" ,"Sqlite"]
17+
---
18+
19+
# Nuget Package
20+
21+
## Create .ipynb file
22+
23+
```csharp
24+
#r "nuget: Faker.Net, 2.0.163"
25+
#r "nuget:SkiaSharp, 3.116.1"
26+
#r "nuget: System.Data.SQLite.Core, 1.0.119"
27+
```
28+
29+
## Importing namespace
30+
31+
```csharp
32+
using Faker;
33+
using System.IO;
34+
using SkiaSharp;
35+
using System;
36+
using System.Collections.Generic;
37+
using System.Reflection;
38+
using System.Data.SQLite;
39+
```
40+
41+
## Create Class
42+
43+
```csharp
44+
public class Book
45+
{
46+
public int Id { get; set; }
47+
public string Title { get; set; }
48+
public string Author { get; set; }
49+
public byte[] Image { get; set; }
50+
public decimal Price { get; set; }
51+
public string Description { get; set; }
52+
public DateTime PublishDate { get; set; }
53+
public string Category { get; set; }
54+
}
55+
```
56+
57+
## Bunch of Color for random background color
58+
59+
```csharp
60+
var listOfColor = new List<SKColor>
61+
{
62+
SKColor.Parse("#86B6F6"),
63+
SKColor.Parse("#176B87"),
64+
SKColor.Parse("#00A9FF"),
65+
SKColor.Parse("#FF90BC"),
66+
SKColor.Parse("#8ACDD7"),
67+
SKColor.Parse("#F2AFEF"),
68+
SKColor.Parse("#C499F3"),
69+
SKColor.Parse("#33186B"),
70+
71+
};
72+
```
73+
74+
## Sqlite Database File Location
75+
76+
```csharp
77+
var connectionString = @"Data Source=C:\Users\91746\source\repos\Shopping\Shopping\BookStore.db";
78+
```
79+
80+
## This will Generate Data for Cover Image Dynamically
81+
82+
```csharp
83+
byte[] Generate<T>(T book)
84+
{
85+
int width = 480;
86+
int height = 540;
87+
int marginY = -10;
88+
int marginX = -10;
89+
90+
string mainText =Faker.Name.First(); //book.Title;
91+
string subText = Faker.Name.Last();
92+
93+
string backGroundColor =listOfColor[Faker.RandomNumber.Next(0,listOfColor.Count()-1)].ToString();
94+
string textColor = "#ffffff";
95+
string boderColor = "#ffffff";
96+
SKBitmap bmp = new(width, height);
97+
SKCanvas canvas = new(bmp);
98+
canvas.Clear(SKColor.Parse(backGroundColor));
99+
using (var paint = new SKPaint())
100+
{
101+
paint.TextSize = width/ 10.0f;
102+
paint.IsAntialias = true;
103+
paint.Color = SKColor.Parse(textColor);
104+
paint.IsStroke = false;
105+
paint.StrokeWidth = 3;
106+
paint.TextAlign = SKTextAlign.Center;
107+
canvas.DrawText(mainText, width / 2f, height / 2f, paint);
108+
paint.TextSize = width/ 25.0f;
109+
paint.TextAlign = SKTextAlign.Right;
110+
canvas.DrawText(subText, width+marginX, height+marginY, paint);
111+
paint.TextSize = width/ 20.0f;
112+
paint.IsStroke = true;
113+
paint.TextAlign = SKTextAlign.Center;
114+
paint.Color = SKColor.Parse(textColor);
115+
}
116+
//SKFileWStream fs = new($"Images/{book.Title}.jpg");
117+
//bmp.Encode(fs, SKEncodedImageFormat.Jpeg, quality: 50);
118+
bmp.Encode(SKEncodedImageFormat.Jpeg,100);
119+
using (MemoryStream ms = new MemoryStream())
120+
{
121+
bmp.Encode(ms, SKEncodedImageFormat.Jpeg, 100);
122+
return ms.ToArray();
123+
}
124+
return bmp.Bytes;
125+
}
126+
```
127+
128+
## Using Reflection For Auto filling Data To Proprties
129+
130+
```csharp
131+
T GetObjectOf<T>()
132+
{
133+
Type objectType = typeof(T);
134+
object objectObject = Activator.CreateInstance(objectType);
135+
136+
// Get the properties of the Book class
137+
PropertyInfo[] properties = objectType.GetProperties();
138+
139+
// Use Faker.NET to populate the properties dynamically
140+
foreach (var property in properties)
141+
{
142+
// Skip the 'Id' property as it's usually auto-generated
143+
if (property.Name == "Id")
144+
continue;
145+
146+
// Create fake data based on the property type
147+
if (property.PropertyType == typeof(string))
148+
{
149+
property.SetValue(objectObject, Faker.Name.FullName());
150+
}
151+
else if (property.PropertyType == typeof(int))
152+
{
153+
// Assign a random integer
154+
property.SetValue(objectObject, Faker.RandomNumber.Next());
155+
}
156+
else if (property.PropertyType == typeof(decimal))
157+
{
158+
// Assign a random decimal value
159+
property.SetValue(objectObject, (decimal)(Faker.RandomNumber.Next(01,1000) ));
160+
}
161+
else if (property.PropertyType == typeof(DateTime))
162+
{
163+
// Assign a random past date
164+
property.SetValue(objectObject, DateTime.Now.AddMonths(Faker.RandomNumber.Next(1,100)));
165+
}
166+
else if (property.PropertyType == typeof(byte[]))
167+
{
168+
// Assign a random byte array (representing an image or file)
169+
property.SetValue(objectObject, Generate((T)objectObject));
170+
}
171+
else if (property.PropertyType == typeof(System.Enum))
172+
{
173+
// For enum types, assign a random enum value if the property is of enum type
174+
Array enumValues = property.PropertyType.GetEnumValues();
175+
var randomEnumValue = enumValues.GetValue(Faker.RandomNumber.Next(0, enumValues.Length));
176+
property.SetValue(objectObject, randomEnumValue);
177+
}
178+
}
179+
return (T)objectObject;
180+
}
181+
```
182+
183+
## Generate Bunch Of Book Will bw used to insert to database
184+
185+
```csharp
186+
List<T> GeBook<T>()
187+
{
188+
var ls = new List<T>();
189+
for(var i=0;i<50;i++)
190+
{
191+
var newBook = GetObjectOf<T>();
192+
ls.Add(newBook);
193+
}
194+
return ls;
195+
}
196+
```
197+
198+
## Dynamically Creating SqlQuery string Using Reflection
199+
200+
```csharp
201+
public void InsertBook<T>(T entity, string queryString)
202+
{
203+
string insertQuery = queryString;
204+
try
205+
{
206+
using (SQLiteConnection conn = new SQLiteConnection(connectionString))
207+
{
208+
try
209+
{
210+
conn.Open();
211+
using (SQLiteCommand cmd = new SQLiteCommand(insertQuery, conn))
212+
{
213+
Type type = typeof(T);
214+
// Get all properties of the Book class using Reflection
215+
PropertyInfo[] properties = type.GetProperties();
216+
foreach (var property in properties)
217+
{
218+
// Get the name of the property
219+
string propertyName = property.Name;
220+
object propertyValue = property.GetValue(entity);
221+
cmd.Parameters.AddWithValue($"@{propertyName}", propertyValue);
222+
}
223+
var num = cmd.ExecuteNonQuery();
224+
}
225+
}
226+
catch(Exception ex)
227+
{
228+
Console.WriteLine(ex.Message);
229+
}
230+
}
231+
}
232+
catch(Exception ex)
233+
{
234+
Console.WriteLine(ex.Message);
235+
}
236+
237+
}
238+
```
239+
240+
## Here data is inserted to database
241+
242+
```csharp
243+
string InsertCommandStringGenerator<T>()
244+
{
245+
Type bookType = typeof(T);
246+
247+
// Get all the properties of the Book class
248+
PropertyInfo[] properties = bookType.GetProperties();
249+
250+
// Initialize StringBuilder to construct the SQL query
251+
StringBuilder insertQuery = new StringBuilder();
252+
253+
// Start building the SQL query
254+
insertQuery.AppendLine("INSERT INTO Books (");
255+
256+
// Loop through the properties to add column names
257+
for (int i = 0; i < properties.Length; i++)
258+
{
259+
if(properties[i].Name.ToLower()=="id") continue;
260+
insertQuery.Append(properties[i].Name);
261+
262+
if (i < properties.Length - 1)
263+
{
264+
insertQuery.Append(", ");
265+
}
266+
}
267+
268+
insertQuery.AppendLine(") VALUES (");
269+
270+
// Loop through the properties again to add parameter placeholders
271+
for (int i = 0; i < properties.Length; i++)
272+
{
273+
if(properties[i].Name.ToLower()=="id") continue;
274+
insertQuery.Append("@");
275+
insertQuery.Append(properties[i].Name);
276+
if (i < properties.Length - 1)
277+
{
278+
insertQuery.Append(", ");
279+
}
280+
}
281+
insertQuery.AppendLine(");");
282+
return insertQuery.ToString();
283+
}
284+
```
285+
286+
## Final Command To populate all above
287+
288+
```csharp
289+
var queryString = InsertCommandStringGenerator<Book>();
290+
foreach(var item in GeBook<Book>())
291+
{
292+
InsertBook(item,queryString);
293+
}
294+
```
+2
Loading

content/blog/general/streamvideo.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ keywords: [ "NET", "codefrydev", "C sharp", "CFD" ,"StreamVideo"]
1919

2020
## Stream Video in Asp.net Core with seeking Function
2121

22-
- Create One Controller CLass and Paste below code in side that class.
22+
- Create One Controller CLass and Paste below code in side that class.
23+
2324
```csharp
2425
[HttpGet("{fileName}")]
2526
public IActionResult GetMedia(string fileName)
@@ -64,4 +65,4 @@ public IActionResult GetMedia(string fileName)
6465

6566
return new FileStreamResult(responseStream, mimeType);
6667
}
67-
```
68+
```

0 commit comments

Comments
 (0)