🙂 İNSANLARIN EN HAYIRLISI INSANLARA FAYDALI OLANDIR 🙂

Ramazan HABER / Dapper / Dapper Kullanımı yeni

-2) id ye göre çekme ->   var cihazOlan = dapperTool.Get<cihaz>(1);

-1-) Dapper - Foreach ile IEnumerable dolaşma

 

string sql = "select top 1 RHAyarlar_id from RHAyarlar";

IEnumerable<RHAyarlar> ogrenci_nesne = dapperToolRmosBaglanti.Query<RHAyarlar>(sql);

 foreach (var item in ogrenci_nesne)

            {

              string  RHAyarlar_id = item.RHAyarlar_id.ToString();

            }

veya

            IEnumerable<notlar1> notlar1 = dapperTool.GetAll<notlar1>();

            foreach (var item in notlar1.AsList<DapperOrnek.notlar1>())

            {

                string baslik = item.not_basligi;

                Console.WriteLine(baslik);

            }

 

0-) companentler txtId,txtAd,txtYas,gridcontrol1,

1-) Dapper - NuGet Managerdan dapper ve Dapper.Contrib  yükle

2-) System.Configuration .dll sini referancesdan ekle

3-)  app.config aşağıdaki gibi olsun

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

<configuration>

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

 

  <connectionStrings>

    <add name="myConnectionString" connectionString="Server=.;Database=deneme;User ID=sa;Password=123;" providerName="System.Data.SqlClient"/>

  </connectionStrings>

</configuration>

4-) modelim veri tabanındaki ile aynı ogrenci.cs

using Dapper.Contrib.Extensions;

namespace DapperOrnek

{

    [Table("ogrenci")]

    public class ogrenci

    {

        [Key]

        public int ogrenci_id { get; set; }

 

        public string ogrenci_ad { get; set; }

 

        public string ogrenci_yas { get; set; }

 

    }

}

5-) Load kısmı

public DapperTools dapperTool;

        public Form1()

        {

            InitializeComponent();

            dapperTool = new DapperTools(Constant.dapperContext);

        }

6-) Select çekme ve datatableye atma

 public void ListeleTool_ile()

        {

            //1.yol

            IEnumerable<ogrenci> ogrenci_nesne = dapperTool.GetAll<ogrenci>();

            gridControl1.DataSource = ogrenci_nesne;

            // 2.yol

            // IEnumerable<ogrenci> ogrenci_nesne = dapperTool.Query<ogrenci>("select * from ogrenci");

            // gridControl1.DataSource = ogrenci_nesne;

        }

7-) insert yapma

public void KaydetTool_ile()

        {

            ogrenci ogrenci = new ogrenci();

            ogrenci.ogrenci_id = Convert.ToInt32(txtId.Text);

            ogrenci.ogrenci_ad = txtAd.Text;

            ogrenci.ogrenci_yas = txtYas.Text;

            dapperTool.Insert(ogrenci);

            Listele();

        }

8-) update

public void GuncelleTool_ile()

        {

            var ogrenci = new ogrenci

            {

                ogrenci_id = Convert.ToInt32(txtId.Text),

                ogrenci_ad = txtAd.Text,

                ogrenci_yas = txtYas.Text,

            };

            dapperTool.Update(ogrenci);

            Listele();

        }

9-) delete

public void SilTool_ile()

        {

            dapperTool.Delete<ogrenci>(new ogrenci { ogrenci_id=Convert.ToInt32(txtId.Text)});

            Listele();

        }

10-) arama ve bulduğunu datatableye atma

  public void AraTool_ile()

        {

            string sql = "SELECT * FROM ogrenci WHERE ogrenci_id IN @ogrenci_id";

            string[] aranacaklar = txtId.Text.Split(',');

            IEnumerable<ogrenci> ogrenci_nesne = dapperTool.Query<ogrenci>(sql, new { ogrenci_id = aranacaklar }); // 1, 2, 3, 4, 5

            gridControl1.DataSource = ogrenci_nesne;

 // 2.yol

            // ogrenci ogrenci_nesne2 = dapperTool.Get<ogrenci>(txtId.Text);

        }

11-) Dapper - class to Datatable metodu

 public static DataTable CreateDataTable<T>(IEnumerable<T> list)

        {

            Type type = typeof(T);

            var properties = type.GetProperties();

 

            DataTable dataTable = new DataTable();

            foreach (PropertyInfo info in properties)

            {

                dataTable.Columns.Add(new DataColumn(info.Name, Nullable.GetUnderlyingType(info.PropertyType) ?? info.PropertyType));

            }

 

            foreach (T entity in list)

            {

                object[] values = new object[properties.Length];

                for (int i = 0; i < properties.Length; i++)

                {

                    values[i] = properties[i].GetValue(entity);

                }

 

                dataTable.Rows.Add(values);

            }

 

            return dataTable;

        }

 

11-) Dapper - SQL to Class

declare @TableName sysname = 'Tablo ismini buraya yaz'

declare @Result varchar(max) = 'public class ' + @TableName + '

{'

 

select @Result = @Result + '

    public ' + ColumnType + NullableSign + ' ' + ColumnName + ' { get; set; }

'

from

(

    select

        replace(col.name, ' ', '_') ColumnName,

        column_id ColumnId,

        case typ.name

            when 'bigint' then 'long'

            when 'binary' then 'byte[]'

            when 'bit' then 'bool'

            when 'char' then 'string'

            when 'date' then 'DateTime'

            when 'datetime' then 'DateTime'

            when 'datetime2' then 'DateTime'

            when 'datetimeoffset' then 'DateTimeOffset'

            when 'decimal' then 'decimal'

            when 'float' then 'float'

            when 'image' then 'byte[]'

            when 'int' then 'int'

            when 'money' then 'decimal'

            when 'nchar' then 'string'

            when 'ntext' then 'string'

            when 'numeric' then 'decimal'

            when 'nvarchar' then 'string'

            when 'real' then 'double'

            when 'smalldatetime' then 'DateTime'

            when 'smallint' then 'short'

            when 'smallmoney' then 'decimal'

            when 'text' then 'string'

            when 'time' then 'TimeSpan'

            when 'timestamp' then 'DateTime'

            when 'tinyint' then 'byte'

            when 'uniqueidentifier' then 'Guid'

            when 'varbinary' then 'byte[]'

            when 'varchar' then 'string'

            else 'UNKNOWN_' + typ.name

        end ColumnType,

        case

            when col.is_nullable = 1 and typ.name in ('bigint', 'bit', 'date', 'datetime', 'datetime2', 'datetimeoffset', 'decimal', 'float', 'int', 'money', 'numeric', 'real', 'smalldatetime', 'smallint', 'smallmoney', 'time', 'tinyint', 'uniqueidentifier')

            then '?'

            else ''

        end NullableSign

    from sys.columns col

        join sys.types typ on

            col.system_type_id = typ.system_type_id AND col.user_type_id = typ.user_type_id

    where object_id = object_id(@TableName)

) t

order by ColumnId

 

set @Result = @Result  + '

}'

 

print @Result

 

 

 2021 Mart 08 Pazartesi
 485