C# projem için sql veri tabanı kuruyorum ve veri tabanı dosyasını Program Files'te bulunan SQL Server dosyası içersine otomatik olarak yerleştiriyor. Böyle olunca da iş uzuyor. Hizmetlerden SQL Serveri'ı durdurup veri tabanını proje dosyalarının içine koymak ardından Server'ı tekrardan başlatmak ve kodların adreslerini değiştirmek uzun sürüyor. Benim istediğim ise direkt olarak veri tabanını proje dosyasının içine nasıl kurabilirim. -ŞİMDİDEN TEŞEKKÜRLER
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.
hyrgulH
kullanıcısına yanıt
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 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.
hyrgulH
kullanıcısına yanıt
Sağolun hocam çok açıklayıcı olmuş
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 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(); }
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(); }
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
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 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 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; }
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.
yeni mesaja git
Yeni mesajları sizin için sürekli kontrol ediyoruz, bir mesaj yazılırsa otomatik yükleyeceğiz.Bir Daha Gösterme