我創建3個表:無法得到此表更新,SQL Server數據庫C#應用程序
Clients
:CLIENT_ID,CLIENT_NAME,Client_Status,Employees
:EMP_ID,EMP_NAME,Emp_RoleEmpJobs
: Emp_ID,Emp_Name,Client_ID,Client_Name,Hours_Spent,Job_Date
我試圖插入/更新數據(Hours_Spent
,Job_Date
)至EmpJobs
,其基於Clients
和Employees
中的相關列。
這裏是我的代碼
string constring = "Data Source=baker-pc;Initial Catalog=BakodahDB;Integrated Security=True";
string sqlQuery = "UPDATE EmpJobs SET [email protected]_Spent, Job_Date [email protected]_Date" +
" WHERE [email protected]_Name AND [email protected]_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!");
它不會顯示在Visual Studio中的任何錯誤,但是當我檢查數據庫沒有什麼數據沒有被插入到表中。有什麼問題?
你不能用UPDATE語句插入數據。它只能更新EXISTING數據 –
如果你正在尋找upsert然後谷歌sql合併 – Steve