1-) C# - ileri programlama final 1-)DLL,MSSQL,semaphore,lock ile ilgili örnek
1-)
MSSQL
e BAĞLANTI EN KOLAY YOLU EZBERLE
button_click(....){
try{
SqlConnection conn = null;//VERİ
TABANINA BAĞLANTI İÇİN
conn = new SqlConnection(@"Data
Source=RAMBO;Initial Catalog=ileriprog;User ID=sa;Password=123;");
conn.Open();
SqlCommand sqlkomut = null;
sqlkomut = new SqlCommand("select
isim from al", conn);
SqlDataReader dr = null;
dr = sqlkomut.ExecuteReader();
while (dr.Read())
{
string isim = dr["isim"] +
"";
"";
}}
catch (Exception E)
{
Console.Write("hata burada
" + E.Message);
" + E.Message);
}
finally
{
conn.Close();
}
}
2-)SORU
SQL
deki isim alanında dll1,dll2,…dll100 kadar 100 adet dll vardir
Her
biri için bir tane thread çalıştıran yani 100 tane thread çalıştıracak
Ve
her bir dll yi dinamik bir şekilde yükleyen programı yazın
Lock
veya semaphore kullanın
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ileriveritab
{
public partial class Form1 : Form
{
SqlConnection conn = null;//VERİ TABANINA BAĞLANTI İÇİN
Thread thread;
public object locker = new object();
Semaphore sem = new Semaphore(5, 10);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
dllcek();
}
private void dllcek()
{
System.Windows.Forms.Form.CheckForIllegalCrossThreadCalls
=
= false;
try
{
conn = new SqlConnection(@"Data Source=RAMBO;Initial Catalog=ileriprog;User
ID=sa;Password=123;"
ID=sa;Password=123;");
//Data
Source = server name
Source = server name
//initial
catalog= veri tabanı ismi
catalog= veri tabanı ismi
//User
ID = bağlantı için id
ID = bağlantı için id
//
Password = bağlantı için şifre
Password = bağlantı için şifre
conn.Open();
SqlCommand sqlkomut = null;
sqlkomut = new SqlCommand("select isim from al", conn);
SqlDataReader dr = null;
dr = sqlkomut.ExecuteReader();
while (dr.Read())
{
string isim = dr["isim"] + "";//MSSQL
isim alaninda dll1…dll100 var
isim alaninda dll1…dll100 var
thread = new Thread(() =>
calistir(isim));//metoda dll1-dll100 gonder
calistir(isim));//metoda dll1-dll100 gonder
thread.Start();//dll sayisi
kadar thread calisir
kadar thread calisir
}
}
catch (Exception E)
{
Console.Write("hata burada
"
" + E.Message);
}
finally
{
conn.Close();
}
}
public void calistir(string isim)//isim=dll1,dll2,dll3 vs… dll100
{
// lock
(locker)
(locker)
sem.WaitOne();
try
{
textBox1.Text = textBox1.Text +
"\n" + isim;
var DLL = Assembly.LoadFile(@"D:\DLLLER\" + isim + ".dll");
Type type = DLL.GetType(isim + "." + "Class1");//namespaca.class
dynamic c = Activator.CreateInstance(type);
c.ac();//dlllerdeki aynı isimli
metod
metod
Thread.Sleep(1000);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
sem.Release();
}
}