VS2022(C#) net6 CS_SQLite_test project [MVC架構範例]
VS2022(C#) net6 CS_SQLite_test project [MVC架構範例]
資料來源: https://www.youtube.com/watch?v=ayp3tHEkRc0
https://sqlitebrowser.org/dl/ [軟體:SQLiteDatabaseBrowserPortable]
GITHUB: https://github.com/jash-git/VS2022-net6-CS_SQLite_test
code[Model\PersonModel.cs]
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CS_SQL_test { public class PersonModel { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string FullName { get { return $"{ FirstName } { LastName }"; } } } }
code[Model\SqliteDataAccess.cs]
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.SQLite;//SQLiteConnection using Dapper;//DynamicParameters using System.Data;//IDbConnection using System.Configuration;//ConfigurationManager namespace CS_SQL_test { public class SqliteDataAccess { public static List<PersonModel> LoadPeople() { using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString())) { var output = cnn.Query<PersonModel>("select * from Person", new DynamicParameters()); cnn.Close(); return output.ToList(); } } public static void SavePerson(PersonModel person) { using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString())) { cnn.Execute("insert into Person (FirstName, LastName) values (@FirstName, @LastName)", person); } } private static string LoadConnectionString(string id = "Default") { return ConfigurationManager.ConnectionStrings[id].ConnectionString; } } }
code[View\Form1View.cs]
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CS_SQL_test { public partial class Form1 : Form { private void button1SetView() { textBox1.Text = ""; textBox2.Text = ""; } private PersonModel button1GetData() { PersonModel person=new PersonModel(); person.FirstName = textBox1.Text; person.LastName = textBox2.Text; return person; } private void listBox1Show() { listBox1.Items.Clear(); List<PersonModel> LPM_data = SqliteDataAccess.LoadPeople(); if(LPM_data.Count>0) { for(int i = 0; i < LPM_data.Count; i++) { listBox1.Items.Add(LPM_data[i].FullName); } }//if(LPM_data.Count>0) else { // }//if (LPM_data.Count > 0) else } } }
code[Form1.Designer.cs]
namespace CS_SQL_test { partial class Form1 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.textBox1 = new System.Windows.Forms.TextBox(); this.textBox2 = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.listBox1 = new System.Windows.Forms.ListBox(); this.SuspendLayout(); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(81, 62); this.textBox1.Margin = new System.Windows.Forms.Padding(4); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(165, 34); this.textBox1.TabIndex = 0; // // textBox2 // this.textBox2.Location = new System.Drawing.Point(81, 126); this.textBox2.Margin = new System.Windows.Forms.Padding(4); this.textBox2.Name = "textBox2"; this.textBox2.Size = new System.Drawing.Size(165, 34); this.textBox2.TabIndex = 1; // // button1 // this.button1.Location = new System.Drawing.Point(81, 189); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(165, 29); this.button1.TabIndex = 2; this.button1.Text = "button1"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // listBox1 // this.listBox1.FormattingEnabled = true; this.listBox1.ItemHeight = 25; this.listBox1.Location = new System.Drawing.Point(297, 62); this.listBox1.Name = "listBox1"; this.listBox1.Size = new System.Drawing.Size(307, 254); this.listBox1.TabIndex = 3; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(12F, 25F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(634, 356); this.Controls.Add(this.listBox1); this.Controls.Add(this.button1); this.Controls.Add(this.textBox2); this.Controls.Add(this.textBox1); this.Font = new System.Drawing.Font("微軟正黑體", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); this.Margin = new System.Windows.Forms.Padding(4); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); this.PerformLayout(); } #endregion private TextBox textBox1; private TextBox textBox2; private Button button1; private ListBox listBox1; } }
code[Form1.cs]
namespace CS_SQL_test { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { listBox1Show(); } private void button1_Click(object sender, EventArgs e) { SqliteDataAccess.SavePerson(button1GetData()); listBox1Show(); button1SetView(); } } }
code[App.config]
<?xml version="1.0" encoding="utf-8" ?> <configuration> <connectionStrings> <add name="Default" connectionString="Data Source=.\DemoDB.db;Version=3;" providerName="System.Data.SqlClient" /> </connectionStrings> </configuration>
完整YOUTUBE教學影片備份:
3 thoughts on “VS2022(C#) net6 CS_SQLite_test project [MVC架構範例]”
測試VS2022 C#的.net6 使用NuGet安裝 System.Data.SQLite.Core(1.0.115.5),Dapper(2.0.123)元件
System.Data.SQLite.Core
版本1.0.115.5
作者: SQLite Development Team
專案URL: https://system.data.sqlite.org/
Dapper
版本2.0.123
作者:Sam Saffron,Marc Gravell,Nick Craver
專案URL: https://github.com/DapperLib/Dapper