Reputation: 39
I created 3 tables:
Clients
: Client_ID, Client_Name, Client_Status, Employees
: Emp_ID, Emp_Name, Emp_Role EmpJobs
: Emp_ID, Emp_Name, Client_ID, Client_Name, Hours_Spent, Job_Date I'm trying to insert/update data (Hours_Spent
, Job_Date
) to EmpJobs
based on related columns in Clients
and Employees
.
Here's my code
string constring = "Data Source=baker-pc;Initial Catalog=BakodahDB;Integrated Security=True";
string sqlQuery = "UPDATE EmpJobs SET Hours_Spent=@Hours_Spent, Job_Date =@Job_Date" +
" WHERE Client_Name=@Client_Name AND Emp_Name=@Emp_Name";
SqlConnection conDataBase = new SqlConnection(constring);
SqlCommand sqlCommand = new SqlCommand(sqlQuery, conDataBase);
conDataBase.Open();
sqlCommand.CommandText = sqlQuery;
sqlCommand.Parameters.Add("@Hours_Spent",SqlDbType.Int).Value = comboBox3.SelectedItem;
sqlCommand.Parameters.Add("@Job_Date",SqlDbType.Date).Value = Convert.ToDateTime(dateTimePicker1.Text);
sqlCommand.Parameters.Add("@Client_Name",SqlDbType.VarChar).Value = comboBox1.SelectedItem;
sqlCommand.Parameters.Add("@Emp_Name",SqlDbType.VarChar).Value = comboBox2.SelectedItem;
sqlCommand.ExecuteNonQuery();
conDataBase.Close();
MessageBox.Show("Saved!");
It doesn't show any errors on Visual Studio but when I check the database there's nothing the data wasn't inserted into the tables. What's the problem?
Upvotes: 0
Views: 73
Reputation: 216293
If you don't know if a record exists for that ClientName and EmployeeName and you want to INSERT (if the record doesn't exist) or UPDATE (if the record exists) then you can use a single T-SQL statement called MERGE
string sqlQuery = @"MERGE EmpJobs T
USING (SELECT @Client_Name AS CName, @Emp_Name As EName) as S
ON T.Client_Name = S.CName AND T.Emp_Name = S.EName
WHEN NOT MATCHED THEN INSERT
(Hours_Spent, Job_Date) VALUES (@Hours_Spent, @Job_Date)
WHEN MATCHED THEN UPDATE
SET Hours_Spent=@Hours_Spent, Job_Date =@Job_Date;";
Merge command is followed by the target table name and its alias (EmpJobs T) then a pseudo record is built using the two parameters required to search for record existance. This pseudo record is matched against the target table. At this point you can write the statement to use if a match is found (UPDATE SET ....) or not (INSERT .... VALUES) Do not forget the final semi-colon.
Upvotes: 1
Reputation: 38023
You really shouldn't repeat values (Client_Name, Emp_Name) across tables. I recommend checking out Louis Davidson's slides on database design at http://www.drsql.org/Pages/Presentations.aspx.
Merge is an option, or you can go with an upsert. https://samsaffron.com/blog/archive/2007/04/04/14.aspx.
That said, something like this might work:
string sqlQuery = @"
declare @Client_ID int;
declare @Emp_ID int;
select @Client_ID = Client_ID from dbo.Clients c where c.Client_Name = @Client_Name;
select @Emp_ID = Emp_ID from dbo.Employees e where e.Emp_Name = @Emp_Name;
begin tran;
if exists (select 1 from dbo.EmpJobs j with (updlock,serializable) where j.Client_ID=@Client_ID and j.Emp_ID=@Emp_ID)
begin;
update dbo.EmpJobs set
Hours_Spent = @Hours_Spent
, Job_Date = @Job_Date
where Client_ID = @Client_ID and Emp_ID=@Emp_ID;
end;
else begin;
insert into dbo.EmpJobs (Emp_ID, Emp_Name, Client_ID, Client_Name, Hours_Spent, Job_Date)
select @Emp_ID, @Emp_Name, @Client_ID, @Client_Name, @Hours_Spent, @Job_Date;
end;
commit tran;";
Upvotes: 1