1-) C# RMOS - MSSQL Server LOCALDB Gömülü Veritabanı kullanımı SQL Local DB
-1. ADIM
EĞER YOKSA İNDİR
0.ADIM
1. ADIM
2. ADIM
using System.Data.Entity; using WindowsFormsApp10.Entities;
public class MyDbContext : DbContext { public MyDbContext() : base("name=MyDbConnection") // config dosyasındaki isim { Database.SetInitializer<MyDbContext>(null); // ❗ Veritabanı oluşturma girişimini engeller
}
public DbSet<ogrenci> ogrenci { get; set; } }
|
3. ADIM
4. ADIM
using System.ComponentModel.DataAnnotations.Schema;
namespace WindowsFormsApp10.Entities { [Table("ogrenci")] public class ogrenci { public int Id { get; set; } public string ad { get; set; } public int yas { get; set; } } }
|
5. ADIM app.config
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <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.6.2" /> </startup>
<connectionStrings> <add name="MyDbConnection" connectionString="Data Source=(LocalDB)\MSSQLLocalDB; AttachDbFilename=C:\VeritabaniDosyam\Database1.mdf; Integrated Security=True; Connect Timeout=30; MultipleActiveResultSets=True" providerName="System.Data.SqlClient" /> </connectionStrings>
<entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> <parameters> <parameter value="mssqllocaldb" /> </parameters> </defaultConnectionFactory> <providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> </providers> </entityFramework> </configuration>
|
6.ADIM
using System; using System.Linq; using System.Windows.Forms; using WindowsFormsApp10.Entities; namespace WindowsFormsApp10 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { using (var db = new MyDbContext()) { db.ogrenci.Add(new ogrenci { ad = "ramazan",yas=12 }); db.SaveChanges(); var liste = db.ogrenci.ToList(); gridControl1.DataSource = liste; } } private void button2_Click(object sender, EventArgs e) { using (var db = new MyDbContext()) { var liste = db.ogrenci.ToList(); gridControl1.DataSource = liste; } } } }
|