לא לשכוח לעדכן את הקובץ App.config את השדה attachdbfilename
הקוד
private void empWin_Load(object sender, EventArgs e)
{
List<roleTBL> roleList = (from s in db.roleTBL select s).ToList();
comboBox2.DataSource = roleList;
comboBox2.DisplayMember = "name";
comboBox2.ValueMember = "Id";
update();
}
private void update()
{
List<employeeTBL> listEmp = (from s in db.employeeTBL select s).ToList();
comboBox1.DataSource = listEmp;
comboBox1.DisplayMember = "fullName";
comboBox1.ValueMember = "Id";
dataGridView1.DataSource = listEmp;
}
private void button1_Click(object sender, EventArgs e)
{
string firstName = textBox1.Text;
string lastName = textBox2.Text;
string phone = maskedTextBox1.Text;
string address = textBox3.Text;
// validation
if (firstName == "")
{
MessageBox.Show("יש למלא שם פרטי");
return;
}
employeeTBL e1 = new employeeTBL();
e1.firstName = firstName;
e1.lastName = lastName;
e1.phone = phone;
e1.address = address;
roleTBL r1 = (roleTBL)comboBox2.SelectedItem;
e1.roleID = r1.Id;
e1.isActive = true;
e1.startDate = dateTimePicker1.Value;
db.employeeTBL.Add(e1);
db.SaveChanges();
MessageBox.Show("התווסף בהצלחה");
textBox1.Text = "";
textBox2.Text = "";
update();
}
הגדרת הטבלה
CREATE TABLE [dbo].[employeeTBL] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[firstName] NVARCHAR (50) NOT NULL,
[lastName] NVARCHAR (50) NOT NULL,
[phone] NVARCHAR (50) NOT NULL,
[address] NVARCHAR (350) NULL,
[roleID] INT NOT NULL,
[isActive] BIT NOT NULL,
[startDate] DATETIME NOT NULL,
[endDate] DATETIME NULL,
[fullName] AS (([firstName]+' ')+[lastName]),
PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_employeeTBL_roleTBL] FOREIGN KEY ([roleID]) REFERENCES [dbo].[roleTBL] ([Id])
);