1. sayfa
Projenize sağ tıklayarak bir klasör eklersiniz Data isminde. Sonra bu Data klasörüne sağ tıklarsınız->Add->New Item. Oradan da Service-Based-Database seçersiniz. mdf uzantılı dosyanız projenize eklenir. Sonra bu mdf dosyasına çift tıklayarak Server Explorer panelinde tablolarınızı yaparsınız veyahut panelden veritabanınıza sağ tıklayıp New Query ile tabloların SQL kodlarını varsa çalıştırırsınız. |
Hocam sizin söylediğiniz de mantıklı fakat ben Management Studio kullanıyorum ve veri tabanını açarken de onunla açıyorum. Management Studio üzerinden veri tabanı açarken veri tabanının konumunu proje dosyasının içi olarak ayarlayamaz mıyız? Bahsettiğim üzere SQL server dosyalarının içine atıyor. Ayrı bir sorum olacak < Resime gitmek için tıklayın > Sizin dediğiniz gibi yaptığım zaman proje dosyaları arasında mdf uzantılı "veri" dosyasını bulamıyorum. Yanlış mı yaptım? Eğer doğruysa o dosyayı bulup nasıl management studio da açarım? |
Siz benim dediğim gibi proje içinde oluşturunuz. Sonra Management Studio'ya gelip Databases'a sağ tıklayıp Attach... diyerek projedeki veritabanınızı bağlayıp çalışmanıza devam etmeyi deneyiniz. Projenizin C gibi kolay erişilebilir bir dizinde olmasına dikkat ediniz. Kullanıcı dizinindeki veritabanlarını bağlamıyor. bir şey daha C'deki proje klasörünüzün izinlerini Tam Denetim olarak değiştirmeniz gerekebilir. Bağlanan dosyanın adı değişik görünüyor. Ama sorun yok. |
using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Araç { class Araç_Kiralama { SqlConnection baglanti = new SqlConnection("Data Source=desktop-90qof9i;Initial Catalog=Oto_kiralama;Integrated Security=True"); DataTable tablo; public void ekle_sil_guncelle(SqlCommand komut, string sorgu) { baglanti.Open(); komut.Connection = baglanti; komut.CommandText = sorgu; komut.ExecuteNonQuery(); baglanti.Close(); } public DataTable listele(SqlDataAdapter adtr, string sorgu) { tablo = new DataTable(); adtr = new SqlDataAdapter(sorgu,baglanti); adtr.Fill(tablo); baglanti.Close(); return tablo; } } } |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Araç { public partial class frmMüsteriEkleme : Form { Araç_Kiralama araç_kira = new Araç_Kiralama(); public frmMüsteriEkleme() { InitializeComponent(); } private void btnİptal_Click(object sender, EventArgs e) { this.Close(); } private void btnEkle_Click(object sender, EventArgs e) { string cümle = "insert into müsteri(tc,adsoyad,telefon,adres,email) values(@tc,@adsoyad,@telefon,@adres,@email)"; SqlCommand komut2 = new SqlCommand(); komut2.Parameters.AddWithValue("@tc", tBTc.Text); komut2.Parameters.AddWithValue("@adsoyad", tbAdSoyad.Text); komut2.Parameters.AddWithValue("@telefon", tbTelefon.Text); komut2.Parameters.AddWithValue("@adres", tbAdres.Text); komut2.Parameters.AddWithValue("@email", tbEmail.Text); araç_kira.ekle_sil_guncelle(komut2, cümle); foreach (Control item in Controls) if (item is TextBox) item.Text = ""; } |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Araç { public partial class frmMüsteriListeleme : Form { Araç_Kiralama arackiralama = new Araç_Kiralama(); public frmMüsteriListeleme() { InitializeComponent(); } private void btnİptal_Click(object sender, EventArgs e) { this.Close(); } private void frmMüsteriListeleme_Load(object sender, EventArgs e) { YenileListele(); } private void YenileListele() { string cümle = " select * from müsteri"; SqlDataAdapter adtr2 = new SqlDataAdapter(); dataGridView1.DataSource = arackiralama.listele(adtr2, cümle); } private void textBox1_TextChanged(object sender, EventArgs e) { string cümle = " select * from müsteri where tc like '%" + textBox1.Text +"%'"; SqlDataAdapter adtr2 = new SqlDataAdapter(); dataGridView1.DataSource = arackiralama.listele(adtr2, cümle); } private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { DataGridViewRow satir = dataGridView1.CurrentRow; tBTc.Text = satir.Cells[0].Value.ToString(); tbAdSoyad.Text = satir.Cells[1].Value.ToString(); tbTelefon.Text = satir.Cells[2].Value.ToString(); tbAdres.Text = satir.Cells[3].Value.ToString(); tbEmail.Text = satir.Cells[4].Value.ToString(); } private void btnGüncelle_Click(object sender, EventArgs e) { string cümle = "update müsteri set adsoyad=@adsoyad, telefon=@telefon, adres=@adres,email=@email where tc=@tc"; SqlCommand komut2 = new SqlCommand(); komut2.Parameters.AddWithValue("@tc", tBTc.Text); komut2.Parameters.AddWithValue("@adsoyad", tbAdSoyad.Text); komut2.Parameters.AddWithValue("@telefon", tbTelefon.Text); komut2.Parameters.AddWithValue("@adres", tbAdres.Text); komut2.Parameters.AddWithValue("@email", tbEmail.Text); arackiralama.ekle_sil_guncelle(komut2, cümle); foreach (Control item in Controls) if (item is TextBox) item.Text = ""; YenileListele(); } private void btnSil_Click(object sender, EventArgs e) { DataGridViewRow satir = dataGridView1.CurrentRow; string cümle=" delete from müsteri where tc = '"+satir.Cells["tc"].Value.ToString()+"' "; SqlCommand komut2 = new SqlCommand(); arackiralama.ekle_sil_guncelle(komut2, cümle); YenileListele(); } private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { } } } |
use School go Create function ogrenciDersleri() returns @R table ( Name varchar(max), Surname varchar(max), Courses varchar(max) ) as begin Insert into @R Select S.Name,S.Surname,C.Name From Student AS S INNER JOIN StudentCourse AS SC ON S.ID=SC.StudentID INNER JOIN Course AS C ON SC.LecturerCourseID=C.ID Return end Declare @Name varchar(max), @Surname varchar(max) ,@Courses varchar(max) Declare benimCursor Cursor FOR Select *FROM [dbo].[ogrenciDersleri]() open benimCursor Fetch next from benimCursor into @Name,@Surname,@Courses while @@FETCH_STATUS=0 begin print @Name + ' ' + @Surname + ' ' +@Courses fetch next from benimCursor into @Name,@Surname,@Courses end close benimCursor deallocate benimCursor |
use School go create function myTeacher() returns @R table ( Name varchar(max ), Surname varchar(max), Course varchar(max) ) as begin INSERT INTO @R Select L.Name,L.Surname ,C.Name From Lecturer AS L INNER JOIN LecturerCourse AS LC ON L.ID=LC.LecturerID INNER JOIN Course AS C ON LC.CourseID=C.ID return end Declare @Name varchar(max), @Surname varchar(max), @Courses varchar(max) Declare myCursor2 Cursor For Select *From [dbo].[myTeacher]() open myCursor2 Fetch next from myCursor2 into @Name,@Surname,@Courses while @@FETCH_STATUS=0 begin print @Name + '--' + @Surname + '--' + @Courses Fetch next from myCursor2 into @Name,@Surname,@Courses end close myCursor2 deallocate myCursor2 |
MAIN using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using School.Model; using School.Distributor; namespace final { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void bntSubmit_Click(object sender, EventArgs e) { Student temp = new Student(); temp.Name = tbName.Text; temp.Surname = tbSurname.Text; if (RdMale.Checked) { temp.Gender = "M"; } else { temp.Gender = "F"; } temp.BirthDate = datetime.Value; MainDistributor dis = new MainDistributor(); dis.addStudent(temp); datagrid.DataSource = null; datagrid.DataSource = dis.showStudents().ToList(); MessageBox.Show(temp.Id.ToString()); } private void Form1_Load(object sender, EventArgs e) { MainDistributor db = new MainDistributor(); datagrid.DataSource = db.showStudents().ToList(); } } } DB using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using School.Model; using System.Data; using System.Data.SqlClient; namespace School.DbOperations { public class StudentDb { public Student AddNewStudent(Student temp) { SqlConnection con = new SqlConnection("Data Source=LAPTOP-U8V5LEQ3;Initial Catalog=QuizSchool;Integrated Security=True"); try { if (con.State != System.Data.ConnectionState.Open) { con.Open(); } SqlCommand cmd = new SqlCommand(); cmd.CommandText = "INSERT INTO Student (Name,Surname,Gender,BirthDate) VALUES (@Name,@Surname,@Gender,@BirthDate)"; cmd.Connection = con; cmd.Parameters.AddWithValue("Name", temp.Name); cmd.Parameters.AddWithValue("Surname", temp.Surname); cmd.Parameters.AddWithValue("Gender", temp.Gender); cmd.Parameters.AddWithValue("BirthDate", temp.BirthDate); int numberofinserted = cmd.ExecuteNonQuery(); if (numberofinserted > 0) { cmd.CommandText = "SELECT MAX(Id) FROM Student"; object objlastid = cmd.ExecuteScalar(); int lastid=0; if(int.TryParse(objlastid.ToString(),out lastid)) { temp.Id = lastid; return temp; } } return null; } catch (Exception exc) { throw; } finally { con.Close(); } } public List<Student> showStudents() { SqlConnection con = new SqlConnection("Data Source=LAPTOP-U8V5LEQ3;Initial Catalog=QuizSchool;Integrated Security=True"); if(con.State != System.Data.ConnectionState.Open) { con.Open(); } SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandText = "SELECT * FROM Student"; DataTable mydata = new DataTable(); SqlDataAdapter adap = new SqlDataAdapter(); adap.SelectCommand = cmd; adap.Fill(mydata); List<Student> temp = new List<Student>(); foreach(DataRow item in mydata.Rows) { var std = new Student() { Id = Convert.ToInt32(item["Id"].ToString()), Name = item["Name"].ToString(), Surname = item["Surname"].ToString(), Gender = item["Gender"].ToString(), BirthDate = DateTime.Parse(item["BirthDate"].ToString()) }; temp.Add(std); } return temp; } } } DISTRIBUTOR using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using School.Model; using School.DbOperations; namespace School.Distributor { public class MainDistributor { public Student addStudent(Student temp) { StudentDb db = new StudentDb(); var student = db.AddNewStudent(temp); return temp; } public List<Student> showStudents() { StudentDb db = new StudentDb(); List<Student> temp = new List<Student>(); temp = db.showStudents(); return temp; } } } MODEL using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace School.Model { public class Student { public int Id { get; set; } public String Name { get; set; } public String Surname { get; set; } public String Gender { get; set; } public DateTime BirthDate { get; set; } } } |
Declare @ogrenciSayisi int SET @ogrenciSayisi = ( Select Count (") Frın Student ) select @ogrenciSayisi AS [Ogrenci Sayisi] |
eğer sadece yerel makinede kullanılacaksa ağa vesaire kurulmayacaksa embedded database de işini görür mesela sqlce normal sqlin kırpılmış halidir. |
1. sayfa
-ŞİMDİDEN TEŞEKKÜRLER
DH forumlarında vakit geçirmekten keyif alıyor gibisin ancak giriş yapmadığını görüyoruz.
Üye Ol Şimdi DeğilÜye olduğunda özel mesaj gönderebilir, beğendiğin konuları favorilerine ekleyip takibe alabilir ve daha önce gezdiğin konulara hızlıca erişebilirsin.