设置nls_date_format:MM / DD / YY
设置DateTimePicker的格式:DateTimePickerFormat.Custom
据我所知,日期格式仅用于显示日期信息,不会影响它在数据库中的存储。
但是,我无法使用以下查询语句将日期插入表中
query = "INSERT INTO tuser (objectID, userID, userName, password, birthday) values (urObjID.nextval,'" + txtUserID.Text.Trim() + "','" + txtUserName.Text.Trim() + "','" + txtPassword.Text.Trim + "','" + dtpBirthday.Value + "')" 'Insert directly Date value from DateTimePicker
和
query = "INSERT INTO tuser (objectID, userID, userName, password, birthday) values (urObjID.nextval,'" + txtUserID.Text.Trim() + "','" + txtUserName.Text.Trim() + "','" + txtPassword.Text.Trim + "','" + dtpBirthday.Value.ToString("dd/MM/yyyy") + "')" 'Insert string value with format dd/MM/yyyy
但是可以使用以下查询将日期插入表格
query = "INSERT INTO tuser (objectID, userID, userName, password, birthday) values (urObjID.nextval,'" + txtUserID.Text.Trim() + "','" + txtUserName.Text.Trim() + "','" + txtPassword.Text.Trim + "','" + dtpBirthday.Value.ToString("dd-MMM-yyyy") + "')" 'Insert string value with format dd-MMM-yyyy
和
query = "INSERT INTO tuser (objectID, userID, userName, password, birthday) values (urObjID.nextval,'" + txtUserID.Text.Trim() + "','" + txtUserName.Text.Trim() + "','" + txtPassword.Text.Trim + "',TO_DATE('" + dtpBirthday.Value.ToString("dd-MMM-yy") + "'))" 'Insert date value using TO_DATE function
上述语句之间是否有任何差异使查询有效?
答案 0 :(得分:1)
您不应使用任何语句,使用参数和绑定变量。
query = "INSERT INTO tuser (objectID, userID, userName, password, birthday) values (urObjID.nextval, :userID, :userName, :password, :birthday)"
cmd.CommandText = query
cmd.Parameters.Add("userID", OracleDbType.Varchar2, ParameterDirection.Input).Value = txtUserID.Text.Trim()
cmd.Parameters.Add("userName", OracleDbType.Varchar2, ParameterDirection.Input).Value = txtUserName.Text.Trim()
cmd.Parameters.Add("password", OracleDbType.Varchar2, ParameterDirection.Input).Value = txtPassword.Text.Trim
cmd.Parameters.Add("birthday", OracleDbType.Date, ParameterDirection.Input).Value = dtpBirthday.Value