2017-08-13 13 views
0

次のクエリでデータベースを更新しようとしていますが、日付の書式設定に問題があります。私は何をすべきか?Visual StudioでWindowsフォームを使用して日付をフォーマットする

string query = "update employee_info set FirstName ='txtfirstName.Text',LastName ='txtlastName.Text' ,Address1='txt_address', City = 'combo_city' ,Country='combo_Country',ReportsTo='txt_reportTo' WHERE Bday='dtp_birthDate.Value.ToShortDateString()' and HireDate='dtp_hireDate.Value.ToShortDateString()'"; 
+0

[SQLインジェクションの警告](http://msdn.microsoft.com/en-us/library/ms161953%28v=sql.105%29.aspx) - あなたがすべきである**決して** CONCATENATE ***あなたはSQLインジェクションを避けるために**代わりに**パラメータ化されたクエリ**を使用します - [Little Bobby Tables](https://xkcd.com/327/)をチェックしてください –

+0

投稿しないでください問題のタイトルのみの質問 – Felix

答えて

0

かわりに、パラメータ化クエリを使用する必要があります。これにより、where句の文字列を正しくフォーマットする必要がなくなり、SQLインジェクション攻撃の脆弱性を防ぐことができます(https://en.wikipedia.org/wiki/SQL_injection

db接続)。

strQuery = "update employee_info set [email protected], [email protected] WHERE [email protected] and [email protected]"; 
cmd = new SqlCommand(strQuery); 
cmd.Parameters.AddWithValue("@firstName", txtfirstName.Text); 
cmd.Parameters.AddWithValue("@CompanyName", txtLastName.Text); 
cmd.Parameters.AddWithValue("@birthDate", dtp_birthDate.Value); 
cmd.Parameters.AddWithValue("@hireDate", dtp_hireDate.Value); 
cmd.ExecuteNonQuery(); 
関連する問題