Habit Logger v2 – جامعه dev

Habitloggerv2
نمایش داده شدگان پارامتری در ado.net
بارگیری SQLite
“آموزش SQLITE برای مبتدیان – بدون هیچ وقت یک پایگاه داده ایجاد کنید
تنظیم پایگاه داده SQLite در ویژوال استودیو (14/23)
git/github
https://www.theserverside.com/blog/coffee-talk-java-news-stories-and-opinions/how-to-push-an-existing-project-to-github
https://kbroman.org/github_tutorial/pages/init.html
ابتدا یک پرونده .gitignore ایجاد کنید و اضافه کنید .vs/
یک روش کمتری برای اعلام یک آرایه وجود دارد:int[] menuNumbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
بر خلافint[] menuNumbers = new int { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
من توانستم با استفاده از اکشن IntelliSense “استفاده از بیان” استفاده کنید.
مواد بلااستفاده را تمیز کنید (به عنوان مثال ، غیر ضروری using
اظهارات در بالای کد) با کلیک روی نماد جارو در پایین Vs.
هنگام ایجاد یک جدول که از یک کلید خارجی استفاده می کند ، از آن استفاده کنید ON DELETE CASCADE
در پایان اعلامیه کلیدی خارجی. با این کار تمام سوابق موجود در جدول که از آن شناسه کلید خارجی استفاده شده است حذف می شود. به این ترتیب شما سوابق یتیم را در جدول ندارید که دیگر به چیزی مربوط نمی شود. به عنوان مثال:
static void CreateTables(SQLiteConnection connection)
{
using (var command = connection.CreateCommand())
{
command.CommandText = @"
CREATE TABLE IF NOT EXISTS Habits (
Id INTEGER PRIMARY KEY AUTOINCREMENT,
Habit TEXT NOT NULL,
Unit TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS HabitInstances (
Id INTEGER PRIMARY KEY AUTOINCREMENT,
HabitId INTEGER NOT NULL,
Date TEXT NOT NULL,
Quantity INTEGER NOT NULL,
FOREIGN KEY (HabitId) REFERENCES Habits(Id) ON DELETE CASCADE
);";
command.ExecuteNonQuery();
}
}
وقتی DeleteHabit()
روش اجرا می شود ، عادت انتخاب شده و همچنین همه را حذف می کند HabitInstances
سوابق مربوط به آن عادت.
انتخاب ، Ctrl K ، Ctrl F – رفع تورفتگی
از همان اتصال DB برای کل برنامه استفاده نکنید (از منابع استفاده می کند)
using (var connection = new SQLiteConnection($"Data Source={dbPath}"))
{
}
using
قند نحوی است که در پشت صحنه یک بلوک امتحان/گرفتن ایجاد می کند …
try
{
//new connection
//do something
}
catch (Exception e)
{
//handle error here
}
finally
{
connection.Close()
}
مثال:
void AddHabit()
{
using (var connection = new SQLiteConnection($"Data Source={dbPath}"))
{
connection.Open();
Console.Clear();
PrintHabits("Add Habit");
Console.WriteLine("\nEnter the new habit name:");
string? habitName = Console.ReadLine();
Console.WriteLine("\nEnter the unit of measurement (e.g., miles, pages, minutes, etc.)");
string? unit = Console.ReadLine();
using (var command = connection.CreateCommand())
{
command.CommandText = "INSERT INTO Habits (Habit, Unit) VALUES (@habit, @unit)";
command.Parameters.AddWithValue("@habit", habitName);
command.Parameters.AddWithValue("@unit", unit);
command.ExecuteNonQuery();
}
Console.WriteLine("\nHabit added successfully!");
}
}
گرچه فکر می کنم من هنوز هم باز می شوم using
بیانیه خیلی زود در اینجا و ممکن است قبل از انجام کاری که مربوط به DB است ، بهتر باشد پس از بیان کاربر و اعلامیه های متغیر بهتر باشد.