Visual C# .NET and SQLite Database using Entity Framework 6



Visual Studio 2019 Visual C# .NET and SQLite Database using Entity Framework 6
- เขียนโปรแกรม C# กับฐานข้อมูล SQLite โดยใช้ Entity Framework 6 หรือ EF 6

Free C# Source Code by iBasskung.

Configuration File: App.config

<?xml version="1.0" encoding="utf-8"?>

<configuration>

  <configSections>

    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->

    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />

  </configSections>

  <startup>

    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />

  </startup>

  <entityFramework>

    <providers>

      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />

      <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />

      <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />

    </providers>

  </entityFramework>

  <system.data>

    <DbProviderFactories>

      <remove invariant="System.Data.SQLite.EF6" />

      <add name="SQLite Data Provider (Entity Framework 6)" invariant="System.Data.SQLite.EF6" description=".NET Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" />

      <remove invariant="System.Data.SQLite" />

      <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".NET Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />

    </DbProviderFactories>

  </system.data>

  <connectionStrings>

    <add name="mainNorthwindEntities" connectionString="metadata=res://*/edmNorthwind.csdl|res://*/edmNorthwind.ssdl|res://*/edmNorthwind.msl;provider=System.Data.SQLite.EF6;provider connection string=&quot;data source=D:\CSEF6WindowsAppSQLite\CSEF6WindowsAppSQLite\bin\Debug\Northwind.db;version=3;&quot;" providerName="System.Data.EntityClient" />

  </connectionStrings>

</configuration>

Form: Form1.cs

using System;

using System.Linq;

using System.Windows.Forms;


namespace CSEF6WindowsAppSQLite

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }


        mainNorthwindEntities db = new mainNorthwindEntities();


        private void Form1_Load(object sender, EventArgs e)

        {


            var data = from p in db.Products

                       join c in db.Categories on p.CategoryID equals c.CategoryID

                       join s in db.Suppliers on p.SupplierID equals s.SupplierID

                       select new

                       {

                           Product_ID = p.ProductID, ProductName = p.ProductName,

                           Category_ID = c.CategoryID, CategoryName = c.CategoryName, Description = c.Description,

                           Supplier_ID = s.SupplierID, CompanyName = s.CompanyName, ContactName = s.ContactName

                       };

            // Edit your Connection String in the App.config file in the Solution Explorer window.

            if(data.Count() > 0)

            {

                dataGridView1.DataSource = data.ToList();

            }

            else

            {

                dataGridView1.DataSource = null;

                MessageBox.Show("Sorry, No records found.", "C# SQLite Entity Framework 6 : iBasskung.",

                    MessageBoxButtons.OK, MessageBoxIcon.Information);

            }


        }

    }

}


Comments